How to access a specified object's key value in an array of objects?

I need to access the value of name key of the first object in this 2 object array. Using map() returns the value of both objects. How can I access the first one only?

let x = [ {name : 'A', age: 20}, {name : 'B', age: 21} ]
console.log(x.map(a => a.name))

Why use map here?

Do you need an array of only keys?
If so, you can use the Object.keys method.

Or do you want an array of only values?
Then you can use the Object.values method that I linked below.

But if you just want the first object, I wouldn’t bother with an array or loop at all.

console.log(x[0].name) // "A"
1 Like

There is also Object.values if you need that