admin管理员组文章数量:1391947
This is an update to my previous question Find and update in nested json object
Sample Data
TestObj = {
"Categories": [{
"Products": [{
"id": "a01",
"name": "Pine",
"description": "Short description of pine."
},
{
"id": "a02",
"name": "Pine",
"description": "Short description of pine."
},
{
"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."
}]
};
My Function
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] = newValue;
}
}
return obj;
}
called as
getObjects(TestObj, 'id', 'A', 'B');
It works fine if I'm going to update id; since id don't have duplicates. But if I'm updating name all data matching key value pair is updated. But how to constrain it into a particular key-pair value.
What shall i provide to the function to constrain the update scope and how to implement it. Please help me.
Note: The json that I will b manipulating will be dynamically generated so I can't have any hard coded value in the function
This is an update to my previous question Find and update in nested json object
Sample Data
TestObj = {
"Categories": [{
"Products": [{
"id": "a01",
"name": "Pine",
"description": "Short description of pine."
},
{
"id": "a02",
"name": "Pine",
"description": "Short description of pine."
},
{
"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."
}]
};
My Function
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] = newValue;
}
}
return obj;
}
called as
getObjects(TestObj, 'id', 'A', 'B');
It works fine if I'm going to update id; since id don't have duplicates. But if I'm updating name all data matching key value pair is updated. But how to constrain it into a particular key-pair value.
What shall i provide to the function to constrain the update scope and how to implement it. Please help me.
Note: The json that I will b manipulating will be dynamically generated so I can't have any hard coded value in the function
Share Improve this question edited May 23, 2017 at 12:14 CommunityBot 11 silver badge asked Aug 1, 2013 at 11:51 OkkyOkky 10.5k15 gold badges77 silver badges123 bronze badges 6- 1 Make values unique or break the loop once you found one (or a defined amount) – Virus721 Commented Aug 1, 2013 at 12:06
- The values are dynamically generated so I can't, assure that it will be unique. Also I can't break the loop since I may have key-pair value in 1st child but have to update in 2nd child – Okky Commented Aug 1, 2013 at 12:11
-
2
How do you know which child should be updated? I mean, if you rely on key and value only, you have to update all matching pairs, but if you want to update just one of them then there should be an additional constraint. For example if the constraint is
id
, then it should be really easy to implement. – pckill Commented Aug 7, 2013 at 14:05 - You can check whether input source from where you get data to update json is providing some other constraint on basis of which we can make different bination to update json. – Mithlesh Kumar Commented Aug 7, 2013 at 14:30
- I'm not sure I get the point of this function. What is your use case ? – LeGEC Commented Aug 7, 2013 at 16:13
1 Answer
Reset to default 7 +50I think you can somehow use a path to locate the value and then do the update. I got the idea from this post. (answered by @shesek)
var getPath = function (obj, path, newValue) {
var parts = path.split('.');
while (parts.length > 1 && (obj = obj[parts.shift()]));
obj[parts.shift()] = newValue;
return obj;
}
console.log(getPath(TestObj, 'Categories.0.Products.1.id', 'AAA'))
console.log(TestObj)
So you can pass in the path to the object, for example, if you want to update the id
of the following object to "AAA", you can pass in Categories.0.Products.1.id
{
"id": "a02",
"name": "Pine",
"description": "Short description of pine."
}
then, the object will bee
{
"id": "AAA",
"name": "Pine",
"description": "Short description of pine."
}
Hope it can shed some light on!
本文标签:
版权声明:本文标题:javascript - Find and update in nested json object without changing the same key-pair value in different child path - Stack Over 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744672638a2618918.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论