Object destructuring solution for long arrays?

But what if the array has 100 items and I want the 99’th age ?

Arrays are objects, so this will do:

let {title: lectureTitle, topics: {98: {age: thirdAge}}} = lecture;

Note however that the [...] type of destructuring works with any iterable, whereas {...} only works with objects (and therefore arrays). For the above solution to work with arbitrary iterables you will have to spread the iterable and wrap it with an array.

let {title: lectureTitle, topics: {98: {age: thirdAge}}} = [...lecture];

Leave a Comment