Replace value while iterating through nested json file in javascript

I've searched for a long time but can't find a solution for my problem ... I got a random nested json object like:

var obj = { a: 1, b: 2, c: { a: 1, b: 2, c: {a: 'hello', b: 'HowAreYou?'} } };
someValue = 'hello';
newValue = 'bye';

      

Now I'm going to find "someValue". If "obj" contains this value, a warning will be displayed. Here's my example:

function wannaChangeValue(obj) {
    var itemKey;
    if (obj instanceof Object) {
        for (itemKey in obj) {
            if (obj.hasOwnProperty(itemKey)) {
                wannaChangeValue(obj[itemKey]);
            }
        }
    } else {
        alert(obj);
        if (obj == someValue) {
            alert('got it! now change...')
            //change someValue to newValue 
        }
    }
    return obj
}

wannaChangeValue(obj)

      

This works very well, but how can I change "someValue" to "newValue" and return the entire json file again? I've seen many examples of how to handle this, but I need a solution for any type of nested json object WITHOUT knowing the path to change this value. Perhaps this is a completely wrong approach ... Thanks in advance!

+3


source to share


5 answers


You can use recursion method for any found object, call the function again.



function update(object, search, replace) {
    Object.keys(object).forEach(function (k) {
        if (object[k] && typeof object[k] === 'object') {
            return update(object[k], search, replace)
        }
        if (object[k] === search) {
            object[k] = replace;
        }
    });
    
}

var object = { a: 1, b: 2, c: { a: 1, b: 2, c: {a: 'hello', b: 'HowAreYou?'} } };
    
update(object, 'hello', 'bye');

console.log(object)
      

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

Run codeHide result


+3


source


You can create a function using a loop for...in

, and if the current value matches oldValue, change that value.



var obj = { a: 1, b: 2, c: { a: '1', b: 2, c: {a: 'hello', b: 'HowAreYou?'} } };
var someValue = 'hello';
var newValue = 'bye';

function changeVal(data, oldV, newV) {
  for(var i in data) {
    if(typeof data[i] == 'object') changeVal(data[i], oldV, newV);
    if(data[i] == oldV) data[i] = newV
  }
  return data
}

console.log(changeVal(obj, someValue, newValue))
      

Run codeHide result


+1


source


You can define a function that searches recursively value

through your object and then replace that value.

function replaceObj (obj, valueForSearch,valueForReplace) {
    for (var key in obj) {
        var value = obj[key];
        if (typeof value === 'object') {
            replaceObj(value, valueForSearch,valueForReplace);
        }
        if (value === valueForSearch) {
            obj[key]=valueForReplace;
        }
    }
}
let obj = { a: 1, b: 2, c: { a: 1, b: 2, c: {a: 'hello', b: 'HowAreYou?'} } };
let someValue = 'hello';
let newValue = 'bye';
replaceObj(obj,someValue,newValue);
console.log(obj);
      

Run codeHide result


0


source


This is a quick solution:

function wannaChangeValue(obj) {
    var itemKey;
    if (obj instanceof Object) {
        for (itemKey in obj) {
            if (obj.hasOwnProperty(itemKey)) {
                obj[itemKey] = wannaChangeValue(obj[itemKey]);
            }
        }
    } else {
        alert(obj);
        if (obj == someValue) {
            alert('got it! now change...')
            //change someValue to newValue
            obj = someValue;
        }
    }
    return obj
}

wannaChangeValue(obj)

      

Obviously this will change the value on the object itself, so if you want a completely new object, you'll have to do something different.

0


source


Dude, you just need to change the code like below, it will work for you.

var object = { a: 1, b: 2, c: { a: 1, b: 2, c: {a: 'hello', b: 'HowAreYou?'} } },
        someValue = 'hello',
        newValue = 'bye';
    function wannaChangeValue(obj) {
        var itemKey;
        if (obj instanceof Object) {
            for (itemKey in obj) {
                if (obj.hasOwnProperty(itemKey)) {
                    if (!(obj[itemKey] instanceof Object) && obj[itemKey] == someValue) {
                        alert(someValue+' got it! now change...')
                        //change someValue to newValue
                        obj[itemKey] =newValue;
                        break;
                    }else{
                        wannaChangeValue(obj[itemKey]);
                    }
                }
            }
        }
    }
    wannaChangeValue(object);
    console.log(object)

      

0


source







All Articles