简体   繁体   中英

How can I update a row in a javascript array based on a key value?

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.

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;
    }
}

JSFiddle Example

Using underscore you can use the _.find method to do the following instead of the for loop:

var match = _.find(nameInfo, function(item) { return item.name === name })
if (match) {
    match.age = newNameInfo.age;
}

JSFiddle Example

Edit: You can use ES6 filter combined with arrow functions

nameInfo.filter(x => {return x.name === nametofind })[0].age = newage

You can use _.where function

var match = _.where(nameInfo , {name  :nametofind });

then update the match

match[0].age = newage
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

Using Underscore you can use _.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."}

You can use findWhere and extend

obj = _.findWhere(@songs, {id: id})
_.extend(obj, {name:'foo', age:12});

You can use the _.find method, like this:

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/

var match = _.findWhere(nameInfo , {name  :nametofind });
match.age = newage

A staright forward using lodash's find() function,

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM