How to take a couple of elements from the end of an array using slice?

There is a valid array that I use in the loop. And I need to copy part of the array at each iteration, but from the end. For example, when i = 0 I need to copy part of the array from the end [20, 10,30,10,10,15,35] only this part, to i(20) And so on: i = 1 - I copy [20, 10, 30,10,10,15,35] to i . I can not correctly write inside and out.

 arr = [20,10,30,10,10,15,35] 

    1 answer 1

    Apparently it should be:

    from "i" to "length of Array - i + 1"

    From the end is cut off by a minus sign ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice )

    that is, it will be something like this

     arr.slice(-(длинаМассива - i + 1)); 
    • Thank. It turned out if you take this (-arr.length + i + 1). After minus - Tom
    • Yes, I just wanted to add this - Alexey Shimansky
    • Hmm, I solved the problem and only now I realized that it was not necessary to go from the end. It was possible just from i + 1, and to the end .. You stupidly have 2 hours, and in the end everything is simple. - Tom