How can I update a row in a javascript array based

2019-03-14 14:44发布

问题:

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.

回答1:

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



回答2:

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


回答3:

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



回答4:

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."}


回答5:

You can use findWhere and extend

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


回答6:

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/



回答7:

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


回答8:

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.