'How to use find objects in multiple levels of nested arrays

What I want is to find an object in a nested array, and to get it by a pre-known ScheduleId number, and where the Duration property is defined.

We need to find this element which is contained inside of array of "Columns", and "Columns" are contained within "Table" elements.

After finding this object, I want to update ScheduleId = 0 and Duration = 0.

Sample data:

var data = {
    "Headers": [
        "A",
        "B",
        "C",
        "D"
    ],
    "Table": [
        {
            "Columns": [
                {
                    "Duration": 0,
                    "ScheduleId": 12,
                },
            ],
        },
        {
            "Columns": [
                {
                    "Duration": 22,
                    "ScheduleId": 44,
                },
            ],
        },
        {
            "Columns": [
                {
                    "Duration": 0,
                    "ScheduleId": 1648,
                },
            ],
        },
        {
            "Columns": [
                {
                    "Duration": 0,
                    "ScheduleId": 22,
                },
            ],
        },
    ]
};

Pseudo code:

var requestedScheduleId = 22;
var requestedObj = data.Table.find(x => requestedScheduleId.Equals(x.Columns.ScheduleId) )
requestedObj.ScheduleId = 0;
requestedScheduleId.Duration = 0;

Unsuccessful attempt:

var test = data.Table.map(({ Columns }) => {return Columns = Columns.filter(({ ScheduleId }) => ScheduleId == 22 )});

console.log(test);


Solution 1:[1]

I would not use .map or .filter for this. It's a plain and simple nested loop: For each table, for each column, if condition is met, do something.

Either with for loops:

for (table of data.Table) {
    for (column of table.Columns) {
        if (column.ScheduleId === 22) {
            column.ScheduleId = 0;
            column.Duration = 0;
        }
    }
}

or with Array#forEach:

data.Table.forEach(table => {
    table.Columns.forEach(column => {
        if (column.ScheduleId === 22) {
            column.ScheduleId = 0;
            column.Duration = 0;
        }
    });
});

var data = {
    "Headers": [
        "A",
        "B",
        "C",
        "D"
    ],
    "Table": [
        {
            "Columns": [
                {
                    "Duration": 0,
                    "ScheduleId": 12,
                },
            ],
        },
        {
            "Columns": [
                {
                    "Duration": 22,
                    "ScheduleId": 44,
                },
            ],
        },
        {
            "Columns": [
                {
                    "Duration": 0,
                    "ScheduleId": 1648,
                },
            ],
        },
        {
            "Columns": [
                {
                    "Duration": 0,
                    "ScheduleId": 22,
                },
            ],
        },
    ]
};

var requestedScheduleId = 22;

data.Table.forEach(table => {
    table.Columns.forEach(column => {
        if (column.ScheduleId === requestedScheduleId) {
            column.ScheduleId = 0;
            column.Duration = 0;
        }
    });
});

console.log(data);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1