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];
Probably too late to reply this,
const index = 65
const {title: lectureTitle, topics: {[index]: {age: thirdAge}}} = lecture
because in real life we normally would be using dynamic indices for arrays to destructure, square brackets
instead of numbers
or just { index: {age}}
doesn't work.