I have a very long JSON array
that I fetch using a forEach
loop where I need to display only the last 5 elements.
array.forEach(showOnlyFiveElements => {
//only 5 elements should be show here
});
Can't it be done using a forEach ? Or I should go for something different?
I have a very long JSON array
that I fetch using a forEach
loop where I need to display only the last 5 elements.
array.forEach(showOnlyFiveElements => {
//only 5 elements should be show here
});
Can't it be done using a forEach ? Or I should go for something different?
Share Improve this question asked May 2, 2017 at 11:47 Folky.HFolky.H 1,2044 gold badges16 silver badges40 bronze badges 4 |1 Answer
Reset to default 45You could use Array#slice
with negative value for the last items.
array.slice(-5).forEach()
array.slice(0, 5).forEach
– Tushar Commented May 2, 2017 at 11:48.slice(-5)....
. :) – Tushar Commented May 2, 2017 at 11:49