Skip to content Skip to sidebar Skip to footer

Updating Value Of Multidimensional Array Of Object

If I have array: var arr = [ { text: 'something', child: [ { text: 'something child', child: [..] },.. ] },.. ]; Is ther any more efficient way exept rebuild

Solution 1:

You could iterate the indices and update text property at the end.

functionupdate(child, indices, value) {
    indices.reduce((o, i) => o.child[i], { child }).text = value;
}

var array = [{ text: "something", child: [{ text: "something child", child: [] }] }];

update(array, [0, 0], 'foo');

console.log(array);
.as-console-wrapper { max-height: 100%!important; top: 0; }

Solution 2:

You are probably going to want a recursive method that accepts the array, the indexes, and the new value as input. If the index array is of length one, then it updates that element in the array. If it is not, then it calls the method using the child array at the element specified by the first element in the index array, the index array with its first element removed, and the new value.

Solution 3:

A good alternative is using vanilla for-loop.

let arr = [ {  text: "something",  child: [   {    text: "something child",    child: []   }  ] }],
    index = [0, 0],
    current = undefined;

for(let i = 0; i < index.length; i++) {
  if (i === index.length - 1) current[index[i]].text = "Ele from Stack";
  else current = (current || arr)[index[i]].child;
}

console.log(arr);
.as-console-wrapper { max-height: 100%!important; top: 0; }

Solution 4:

In my opinion, this in quite unmaintainable and you should consider different data structure. However, you asked about this kind of solution. You can you eval(), which is quite working, even it is dangerous for production environment without additional security checks:

var arr = [{
    text: "something",
    child: [{
            text: "something child",
            child: [{
                text: "something child",
                child: [{
                    text: "something child",
                    child: []
                }]
            }, {
                text: "something child",
                child: []
            }]
        },
        {
            text: "something child",
            child: [{
                text: "something child",
                child: []
            }, {
                text: "something child",
                child: []
            }]
        }
    ]
}, {
    text: "something",
    child: [{
            text: "something child",
            child: [{
                text: "something child",
                child: [{
                    text: "something child",
                    child: []
                }]
            }, {
                text: "something child",
                child: []
            }]
        },
        {
            text: "something child",
            child: [{
                text: "something child",
                child: []
            }, {
                text: "something child",
                child: []
            }]
        }
    ]
}];
var ind = [0, 1, 0];
var setsomething = 2;

functionsetChild(ind) {
    var ev = "arr";
    for (var i = 0; i < ind.length; i++) {
        if (i + 1 < ind.length) {
            ev += '[' + ind[i] + ']["child"]';
        } else {
            ev += '[' + ind[i] + ']["text"]'
        }
    }
    ev += '=' + setsomething.toString() + ";";
    console.log(ev);
    console.log("original array");
    console.log(arr);
    eval(ev);
     console.log("Modified array");
    console.log(arr);
}
setChild(ind);

Post a Comment for "Updating Value Of Multidimensional Array Of Object"