DEV Community

Discussion on: 6 Things you probably did not know javascript could do natively

Collapse
 
potouridisio profile image
Ioannis Potouridis

How about using Object Destructuring on arrays?

const data = [
    {
        id: 1,
        name: 'John'
    },
    {
        id: 2,
        name: 'Mary'
    }
]

const { 0: { id }, [data.length - 1]: last, length } = data;

// id => 1
// last => { id: 2, name: 'Mary' }
// length => 2