0

so I have a data on API, all I need is the latest data.

this latest data is always at the bottom, if for example the data is 50, so the latest data is at number 50

my question is, how can I get only the latest data? thank you

devserkan
  • 16,870
  • 4
  • 31
  • 47
ujuy
  • 3
  • 1
  • Does this answer your question? [Get the last item in an array](https://stackoverflow.com/questions/3216013/get-the-last-item-in-an-array) – devserkan May 12 '20 at 06:41
  • 1
    What does this question have to do with `axios` and `reac-hooks`? – Ghasem May 12 '20 at 06:44
  • @AlexJolig sorry I'm still a beginner at react, so I was desperate and gave a title like that – ujuy May 12 '20 at 06:58

1 Answers1

0

You can get it multiple way :

const arr = [5,4,3,2,1];

if(arr.length) {

  console.log("With Length" , arr[arr.length-1])

  console.log("With reverse index" , arr.reverse()[0])

  arr.reverse(); // just to get back normal array

  console.log("With spread pop" , [...arr].pop()) // ES6 without removing array element

  console.log("With spread slice" , [...arr].slice(-1)[0] ) // ES6 without removing array element

  console.log("With pop" ,arr.pop()) // <--- Will remove last element from array
}
Vivek Doshi
  • 56,649
  • 12
  • 110
  • 122