如何根据键值更新 javascript 数组中的一行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16148150/
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
How can I update a row in a javascript array based on a key value?
提问by Richard Dalton
I have an array of data like this:
我有这样的数据数组:
var nameInfo = [{name: "Moroni", age: 50},
{name: "Tiancum", age: 43},
{name: "Jacob", age: 27},
{name: "Nephi", age: 29},
{name: "Enos", age: 34}];
If I have an object like this:
如果我有这样的对象:
var nameInfo = {name: "Moroni", age: 51};
Is there a simple way that I can update the variable nameInfo. The key between these is the name column. I know there is a way that I could do this by searching for the row, removing and adding but I would like to have a way to do this where I updated the row. Note that if it helps I do have underscore.js loaded.
有没有一种简单的方法可以更新变量nameInfo。它们之间的关键是名称列。我知道有一种方法可以通过搜索行、删除和添加来做到这一点,但我想有一种方法可以在更新行的地方做到这一点。请注意,如果有帮助,我确实加载了 underscore.js。
回答by Richard Dalton
Easiest way is to just loop over and find the one with a matching name then update the age:
最简单的方法是循环并找到具有匹配名称的那个,然后更新年龄:
var newNameInfo = {name: "Moroni", age: 51};
var name = newNameInfo.name;
for (var i = 0, l = nameInfo.length; i < l; i++) {
if (nameInfo[i].name === name) {
nameInfo[i].age = newNameInfo.age;
break;
}
}
Using underscore you can use the _.findmethod to do the following instead of the for loop:
使用下划线,您可以使用_.find方法代替 for 循环执行以下操作:
var match = _.find(nameInfo, function(item) { return item.name === name })
if (match) {
match.age = newNameInfo.age;
}
回答by Naruto
Edit:You can use ES6 filtercombined with arrow functions
编辑:您可以将 ES6过滤器与箭头功能结合使用
nameInfo.filter(x => {return x.name === nametofind })[0].age = newage
You can use _.where
function
您可以使用 _.where
功能
var match = _.where(nameInfo , {name :nametofind });
then update the match
然后更新匹配
match[0].age = newage
回答by Uneeb Khan
var nameInfo = [{name: "Moroni", age: 50},{name: "Tiancum", age: 43},
{name: "Jacob", age: 27},{name: "Nephi", age: 29},
{name: "Enos", age: 34}
];
_.map(nameInfo, function(obj){
if(obj.name=='Moroni') {
obj.age=51; // Or replace the whole obj
}
});
This should do it. It's neat and reliable and with underscore
这应该做。它整洁可靠,并带有下划线
回答by Ali
You can use findWhere and extend
您可以使用 findWhere 并扩展
obj = _.findWhere(@songs, {id: id})
_.extend(obj, {name:'foo', age:12});
回答by Thram Cortapapel
Using Underscore you can use _.findWhere http://underscorejs.org/#findWhere
使用下划线,您可以使用 _.findWhere http://underscorejs.org/#findWhere
_.findWhere(publicServicePulitzers, {newsroom: "The New York Times"});
=> {year: 1918, newsroom: "The New York Times",
reason: "For its public service in publishing in full so many official reports,
documents and speeches by European statesmen relating to the progress and
conduct of the war."}
回答by fegemo
You can use the _.find method, like this:
您可以使用 _.find 方法,如下所示:
var nameInfos = [{name: "Moroni", age: 50},
{name: "Tiancum", age: 43},
{name: "Jacob", age: 27},
{name: "Nephi", age: 29},
{name: "Enos", age: 34}];
var nameToSearch = "Moroni";
var myRecord = _.find(nameInfos, function(record){ return record.name === nameToSearch; });
Working example: http://jsfiddle.net/9C2u3/
工作示例:http: //jsfiddle.net/9C2u3/
回答by j1s
A staright forward using lodash's find()function,
使用 lodash 的find()函数直接向前,
var nameInfo = [{name: "Moroni", age: 50},
{name: "Tiancum", age: 43},
{name: "Jacob", age: 27},
{name: "Nephi", age: 29},
{name: "Enos", age: 34}];
var objToUpdate = _.find(nameInfo, {name: "Moroni"});
if(objToUpdate) objToUpdate.age = 51;
console.log(nameInfo); // Moroni age will be 51;
Note: If there are multiple Moroni objects, _.find can fetch the first match only.
注意:如果有多个 Moroni 对象,_.find 只能获取第一个匹配项。
回答by venkatareddy
var match = _.findWhere(nameInfo , {name :nametofind });
match.age = newage