I have an array contains
const data = ['a', 'b', 'c', 'd'];
how to find the last element, result should be 'd'
Using the function slice
+ destructuring assignment.
const data = ['a', 'b', 'c', 'd'],
[last] = data.slice(-1);
console.log(last);
You could slice from the end (negative index) and get the item of the array.
const data = ['a', 'b', 'c', 'd'];
console.log(data.slice(-1)[0]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With