I have an object that is decoded from a json:
var data = [{
"parentSeries":1,
"children":[{
"BusinessRule":"ChrisTest2",
"ID":"ChrisTest2||3",
"childsub":3,
"jsonCondition":{
"parentSeries":1,
"children":[{
"RuleDefinition":"ChrisTest2||3",
"ID":"ChrisTest2||3||CondField1",
"Field":"CondField1",
"Test":"=1"
},{
"RuleDefinition":"ChrisTest2||3",
"ID":"ChrisTest2||3||CondField2",
"Field":"CondField2",
"Test":"=2"
}]
}
}]
}]
I want to update any elements of this object dynamically. I have an array that shows me where the property I want to update is. eg:
var splitMap = ["jsonCondition", "children", "0", "Test"]
I have a value I want to update the final entry in this array with coming in (newValue), and I've got a bit of code to update the particular nested item to contain this new value:
if (splitMap.length > 0) {
var newdata = data;
for (var p = 0; p < splitMap.length-1; p++) {
newdata = newdata[splitMap[p]];
}
newdata[splitMap[splitMap.length - 1]] = newValue;
}
But I can't figure out a way to get this updated into the original data! I basically want to do
oldobject['jsonCondition']['children'][0] = newdata
or
oldobject['jsonCondition']['children'][0]['Test'] = newValue
...but I want it work out the keys and depth of this path based on the content and length of the map array. I have jquery on our platform if that helps ($.each?)! Any ideas? Thanks :)