javascript 如果 json 对象中的键相同,则替换值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31730960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
replace value if key is same in json object
提问by user3399326
I have following array which consist of json objects:
我有以下由 json 对象组成的数组:
items = [{"Id":"car","color":"blue"},{"Id":"truck","color":"red"}]
I have new json object. If the "Id" of this object(newItem) matches the "Id" in items, i would like to replace the "color" value.
我有新的 json 对象。如果此对象(newItem)的“Id”与项目中的“Id”匹配,我想替换“颜色”值。
newItem = {"Id":"car","color":"yellow"}
So, i would like output like this:
所以,我想要这样的输出:
items = [{"Id":"car","color":"yellow"},{"Id":"truck","color":"red"}]
Here's what i have so far:
这是我到目前为止所拥有的:
for (var i=0; i<items.length; i++) {
var x = items[i];
if(x.Id == newItem.Id){ //first check if id matches
if (JSON.stringify(items[i]) == JSON.stringify(newItem)) {
alert('objects are same');
return ;
}
else {
var newColor = JSON.stringify(x).replace(x.color, newItem.color);
alert(JSON.parse(newColor));
}
}
}
回答by Pavan Ravipati
Working JSFiddle. Here's one way to do it by using .forEach
from Array.prototype.forEach:
工作JSFiddle。这是使用.forEach
from Array.prototype.forEach的一种方法:
var items = [{"Id":"car","color":"blue"},{"Id":"truck","color":"red"}];
var newItem = {"Id":"car","color":"yellow"}
items.forEach(function(item) {
if (newItem.Id === item.Id) {
item.color = newItem.color
}
});
回答by GPicazo
Try this:
试试这个:
items.forEach(function(item) {
if(item.Id == newItem.Id && item.color != newItem.color){
item.color = newItem.color;
}
});
回答by Joy
Check working demo: JSFiddle.
检查工作演示:JSFiddle。
Using Array.prototype.map
is concise and clear:
使用Array.prototype.map
简洁明了:
items.map(function(obj) {
(obj.Id === newItem.Id) && (obj.color = newItem.color);
});
回答by Pritam Banerjee
The following code should work for you. You can exit the loop once the id is matched. A little better performance wise.
以下代码应该适合您。一旦 id 匹配,您就可以退出循环。性能好一点。
items = [{"Id":"car","color":"blue"},{"Id":"truck","color":"red"}];
newItem = {"Id":"car","color":"yellow"};
for (var i = 0; i < items.length; i++){
if (newItem.id == items[i].id ){
items[i].color = newItem.color;
break;
}
}
回答by Alexandru Pufan
Here's a way to do it, using jQuery:
这是一种使用jQuery的方法:
var items = [
{"Id": "car", "color": "blue"},
{"Id": "truck", "color": "red"}
];
var newItem = {
"Id": "car",
"color": "yellow"
};
$(items).each(function(item) {
if ($(this)[0].Id == newItem.Id) {
$(this)[0].color = newItem.color
}
});
回答by arahr26
The Underscore.js library isn't strictly necessary for this specific problem, but it's great for dealing with collections.
Underscore.js 库对于这个特定问题并不是绝对必要的,但它非常适合处理集合。
var items = [{"Id":"car","color":"blue"},{"Id":"truck","color":"red"}];
var newItem = {"Id":"car","color":"yellow"};
_.each(items, function(item) {
if (item.Id === newItem.Id) item.color = newItem.color
});