Modifying value in multidimensional array by path in array (or any) format

Wax Cage

I have a multidimensional array representing a potentially unlimited deep category tree menu which looks like this

this.tree = [{
    "title": "1. dragon-breath",
    "items": []
}, {
    "title": "2. moiré-vision",
    "items": [{
        "title": "2.1. tofu-animation",
        "items": [{
            "title": "2.1.1. spooky-giraffe",
            "items": []
            }, {
                "title": "2.1.2. bubble-burst",
                "items": []
            }],
    }, {
        "title": "2.2. barehand-atomsplitting",
        "items": []
    }],
}, {
    "title": "3. unicorn-zapper",
    "items": []
}, {
    "title": "4. romantic-transclusion",
    "items": []
}];

and then I have an array path to one of tree leaves, that dynamically changes in time and can look like this

var path = [0]

next time it can look like this

var path = [1][1]

or this

var path = [1][0][1]

Now I need to change the title attribute of leave represented by this path. I cant find any decent way how to do it. Only thing, that came on my mind was eval function,

eval('tree'+getStringPathByArrayPath([1,0,1])+'.name = "'+updatedName+'"')

which I find kind of evil and also I am using this function as angular ng-model (getter/setter) so I dont want browser to evaluate this over and over.

Is there any decent way how to solve this or do I have a bad approach to this problematic?

PS: I cant just iterate over the path array recursively and get to the right tree leave node by node, because there is no passing by reference and every time I assign one subnode to variable, I get a copy (but I need to change the original).

Thanks for your time.

radiaph

Although Javascript is pass-by-value, the value of any variable holding an object is a reference to the object. Passing such a variable will pass the reference, not a copy of the object. So this will change your tree:

var node = this.tree[path[0]];
for (var i = 1 ; i < path.length ; i++) {
  node = node.items[path[i]];
}
node.title = updatedName;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Modifying value in multidimensional array by path in array (or any) format

From Dev

Format simpe array in multidimensional

From Dev

Search in multidimensional array recursive for key/value pair and return the path

From Dev

Adding to a value in a Multidimensional Array

From Dev

Combine value of multidimensional Array

From Dev

If value is in multidimensional array

From Dev

Access multidimensional array value

From Dev

encoding multidimensional array into json format

From Dev

convert multidimensional array to json format

From Dev

Get the value of array in multidimensional-array in array

From Dev

Get the value of array in multidimensional-array in array

From Dev

Set inner value for multidimensional array

From Java

PHP multidimensional array search by value

From Dev

How to sort multidimensional array by value?

From Dev

Sort multidimensional array by value with a condition

From Dev

PHP Multidimensional Array Value Replacement

From Dev

Multidimensional array - how to get a value

From Dev

Inserting a value in Multidimensional array in PHP

From Dev

check if value exists in multidimensional array

From Dev

php change value of multidimensional array

From Dev

Max value of a multidimensional array javascript

From Dev

Check value in multidimensional associative array

From Dev

list the value of a multidimensional javascript array

From Dev

Multidimensional array sorting by value in php

From Dev

PHP Multidimensional Array Value Replacement

From Dev

return a value from a multidimensional array

From Dev

JS multidimensional array modify value

From Dev

Set inner value for multidimensional array

From Dev

Looking for a value in a multidimensional array in PHP

Related Related

HotTag

Archive