0

I have a nested array of objects i need to change the values of a children data dynamically

[
    {
        type: "root",
        title: "FileManager",
        isDirectory: true,
        children: [
            {
                type: "folder",
                title: "animals",
                isDirectory: true,
                children: [
                    {
                        type: "folder",
                        title: "cat",
                        isDirectory: true,
                        children: [
                            {
                                type: "folder",
                                title: "images",
                                isDirectory: true,
                                children: [
                                    {
                                        type: "file",
                                        title: "cat001.jpg",
                                        isDirectory: false,
                                    }, {
                                        type: "file",
                                        title: "cat002.jpg",
                                        isDirectory: false,
                                    }
                                ]
                            }
                        ]
                    }
                ]
            },
            {
                type : "folder",
                title : 'Birds',
                isDirectory : true,
                children : []
            }
        ]

    }

]

this is my main array of object suppose if i want to change the value of children dynamically. lets take i want to add one more object to the children inside images array so the path of the images array is FileManager / animals / cat / images how can change the value of the children in the images object dynamically?

1 Answers1

2

Based on the data structure provided, you can use a recursive solution to reach the nested array you are interested in and then when the recursion hits the base case, you can either push a new object/s into the array at that specific level of depth or use a callback function to push as many new objects into it.

I am not really sure about the "dynamic" part you are referring to, but the following should place you in the right direction:

function rec(array) {
    for (let i in array) {
        if (array[i].children === undefined) { // BASE CASE
            // console.log("base case ", array);

            // push the object you want into the array, as at this point in the
            // recursion you will be at the level you can modify your image array as you like
            return array.push({ "myImage": "myBeautifulCatImage", "does": "poooooor"});
        }

        // recursive call
        // visualise how deep you are going...
        // console.log("rec", array[i].children); 
        return rec(array[i].children); 
    }
}

rec(arr);

// if you know log your arr as in:
// console.log("arr after recursion", rec(arr))
// you will see your new cat showing where it should be :)

If this answer helps you solving the issue, consider accepting the answer or upvoting it. Thanks.

rags2riches-prog
  • 1,663
  • 1
  • 10
  • 22