Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
75
rated 0 times [  76] [ 1]  / answers: 1 / hits: 22909  / 7 Years ago, tue, may 2, 2017, 12:00:00

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?


More From » foreach

 Answers
2

You could use Array#slice with negative value for the last items.



array.slice(-5).forEach()

[#57929] Sunday, April 30, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
susand

Total Points: 690
Total Questions: 101
Total Answers: 104

Location: Lesotho
Member since Wed, Jun 2, 2021
3 Years ago
;