Solution for object destruction for long arrays?

Looking at this code:

let lecture = {
    id: 2,
    title: "MyTitle",
    topics: [
    {
        title: "John",
        age: 1
    },
    {
        title: "John2",
        age: 2
    },
    {
        title: "John3",
        age: 3
    }]
}

I want to extract the main property titleand the third agein the array (through destructuring the object)

I can do it through :

let { title:lectureTitle , topics:[,,{age:thirdAge}]} = lecture;
console.log(lectureTitle,thirdAge);//MyTitle 3

Question

But what if the array has 100 elements and I want 99'th age?

How would I do that? Is there a solution for object destruction for this?

+4
source share
1 answer

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

Arrays are objects, so this will do:

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

, [...] , {...} (, , ). , ( AFAIK ).

+11

Source: https://habr.com/ru/post/1613536/


All Articles