How to use the spread operator to access element in my array of object ?
const array = [{ obj: 1},{ obj: 2}]
console.log([...array].obj)
// Output undefined
console.log([...array.obj])
// Output Uncaught TypeError
I've seen this post Use spread operator on objects array? which is quite similar but they do not try to access elements.
So is it possible to use spread operator on array of object to access elements ? If so how ?
1and2?[...array]-> clonesarrayinto a new array. You take the.objproperty of that array. Arrays don't have an.objproperty, so you get undefined.[...array.obj]-> spreadarray.objas an array. Since arrays don't have an.objproperty, you're doing an array spread ofundefined. That causes an error.