2013-08-01 170 views
5

我用这个代码从sJhonny's Question查找和嵌套的JSON对象

数据样本

TestObj = { 
    "Categories": [{ 
     "Products": [{ 
      "id": "a01", 
      "name": "Pine", 
      "description": "Short description of pine." 
     }, 
     { 
      "id": "a02", 
      "name": "Birch", 
      "description": "Short description of birch." 
     }, 
     { 
      "id": "a03", 
      "name": "Poplar", 
      "description": "Short description of poplar." 
     }], 
     "id": "A", 
     "title": "Cheap", 
     "description": "Short description of category A." 
    }, 
    { 
     "Product": [{ 
      "id": "b01", 
      "name": "Maple", 
      "description": "Short description of maple." 
     }, 
     { 
      "id": "b02", 
      "name": "Oak", 
      "description": "Short description of oak." 
     }, 
     { 
      "id": "b03", 
      "name": "Bamboo", 
      "description": "Short description of bamboo." 
     }], 
     "id": "B", 
     "title": "Moderate", 
     "description": "Short description of category B." 
    }] 
}; 

功能找到JSON对象所需的部分找到

function getObjects(obj, key, val) { 
    var objects = []; 
    for (var i in obj) { 
     if (!obj.hasOwnProperty(i)) continue; 
     if (typeof obj[i] == 'object') { 
      objects = objects.concat(getObjects(obj[i], key, val)); 
     } else if (i == key && obj[key] == val) { 
      objects.push(obj); 
     } 
    } 
    return objects; 
} 

使用像更新所以:

getObjects(TestObj, 'id', 'A'); // Returns an array of matching objects 

此代码是从源代码中选择匹配的部分。但我想要的是用新值更新源对象并检索更新后的源对象。

我想是这样

getObjects(TestObj, 'id', 'A', 'B'); // Returns source with updated value. (ie id:'A' updated to id:'B' in the returned object) 

我的代码

function getObjects(obj, key, val, newVal) { 
    var newValue = newVal; 
    var objects = []; 
    for (var i in obj) { 
     if (!obj.hasOwnProperty(i)) continue; 
     if (typeof obj[i] == 'object') { 
      objects = objects.concat(getObjects(obj[i], key, val)); 
     } else if (i == key && obj[key] == val) { 
      obj[key] = 'qwe'; 
     } 
    } 
    return obj; 
} 

这工作,如果我给obj[key] = 'qwe';,但如果我的代码变成obj[key] = newValue;其为未定义更新。

这是为什么?

+2

OBJ [关键] =的newval? – Virus721

+0

其他条件是否正确? – Okky

+0

我不明白你想要做什么。您是否想在检索源对象的同时更新源对象? OO – Virus721

回答

7

你忘了通过NEWVALUE在嵌套调用

function getObjects(obj, key, val, newVal) { 
    var newValue = newVal; 
    var objects = []; 
    for (var i in obj) { 
     if (!obj.hasOwnProperty(i)) continue; 
     if (typeof obj[i] == 'object') { 
      objects = objects.concat(getObjects(obj[i], key, val, newValue)); 
     } else if (i == key && obj[key] == val) { 
      obj[key] = 'qwe'; 
     } 
    } 
    return obj; 
} 
+0

哦) – Okky

1
function getObjects(obj, key, val, newVal) { 
    for (var i in obj) { 
     if (!obj.hasOwnProperty(i)) continue; 
     if (i == key && obj[key] == val) { 
      obj[key] = newVal; 
     } 
    } 
    return obj 
} 

这将做的NEWVALUE(的newval)一个发现价值的就地更新

+0

这样可不行!我多么粗心。谢啦 – Okky

1

这?

function update(obj, key, newVal) { 
    for(var i in obj) { 
     if(typeof obj[i] == 'object') { 
      update(obj[i], key, newVal)); 
     } else if(i === key) { 
      obj[i] = newVal; 
     } 
    } 
    return obj; 
} 
+0

不,这是键值对那不是复制键可以是多个副本 – Okky