user1402539
user1402539

Reputation: 225

Look for a value for a given key in JSON and change the value using javascript

I am looking to write a function which can look up a value based on a key and replace that value with another. The key is a tree from the start node of JSON. Here is the example.

var myData = {
    name : 'Dan',
    address: {
        city : 'Santa Clara',
                details : {
                   'prevhouse' : ''
                }
    }
}

Input to the function is a key tree. For eg, myData-address-details-prevhouse

When I pass this key with a new value, say 'Texas', the prevhouse value will get changed to the new value I am sending.

and new JSON will be

var myData = {
    name : 'Dan',
    address: {
        city : 'Santa Clara',
                details : {
                   'prevhouse' : 'Texas'
                }
    }
}

Here is what I wrote so far

var tree = key.split("-");

now the tree variable contains ["myData","address", "details","prevhouse"]

I know that we can access the object using myData[tree[0]][tree[1]][tree[2]], but somehow not able to get it dynamic from parsed value.

how do we generate this dynamically since the length of the depth is not known till runtime.

Hope to get a help.

Upvotes: 2

Views: 2751

Answers (4)

joeyramone76
joeyramone76

Reputation: 241

try with this code:

    var myData = {
        name: 'Dan',
        address: {
            city: 'Santa Clara',
            details: {
                prevhouse: ''
            }
        }
    };


    function setAttribute(obj, key, value) {
        var i = 1,
            attrs = key.split('-'),
            max = attrs.length - 1;

        for (; i < max; i++) {
            attr = attrs[i];
            obj = obj[attr];
        }

        obj[attrs[max]] = value;
        console.log('myData=', myData);

    }

    setAttribute(myData, "myData-address-details-prevhouse", "Texas");

here a working jsfiddle demo; see the console for the result

Upvotes: 4

Sean Kinsey
Sean Kinsey

Reputation: 38046

Something like this would do:

function update(node, path, value) {
  path = path.split('-');
  do {
    node = node[path.splice(0, 1)];
  } while(path.length > 1);
  node[path[0]] = value;
}

Upvotes: 1

Ben Flynn
Ben Flynn

Reputation: 18922

You should be able to iterate through each key because your JSON is just a JS object. So go through each key, check if it's defined, if it is, use that object for your next check. That'll get you where you want to go. Keep in mind you'll be setting the last key to your value.

basic psuedo-code without dealing with setting:

obj = data;
for (key in keys) {
  obj = obj[key]
}

Upvotes: 1

tvanfosson
tvanfosson

Reputation: 532455

Given that myData is the object, I think you should be using myData[tree[1]][tree[2]][tree[3]] and throwing away the first item in the array.

Something like this should work recursively (untested)

function updateValue(obj, key, value)
{
   var keys = key.split('-');
   updateObjectValue(obj, keys.shift(), value);
}

function updateObjectValue(obj, keyArray, value)
{
     if (keyArray.length == 1) {
         obj[keyArray[0]] = value;
     }
     else if (keyArray.length > 1) {
         updateObject(obj[keyArray[0]], keyArray.shift(), value);
     }
}

Upvotes: 0

Related Questions