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
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
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
}