AngularJS Update Array Items by a Given Key

10,015

Solution 1

Use Array#find method to get the array element.

$scope.replyList.find(function(v) {
  return v.id == 85475;
}).reply_content = 'dddddd';

// or with ES6 arrow function 

$scope.replyList.find(v => v.id == 85475).reply_content = 'dddddd';


var replyList = [{
  "id": "85475",
  "reply_content": "aaaaaaa",
  "reply_gender": "1",
  "reply_author": "John"
}, {
  "id": "85487",
  "reply_content": "bbbbbbb",
  "reply_gender": "1",
  "reply_author": "Ben"
}, {
  "id": "85504",
  "reply_content": "ccccccc",
  "reply_gender": "1",
  "reply_author": "Wang"
}];

replyList.find(function(v) {
  return v.id == 85475;
}).reply_content = 'dddddd';

console.log(replyList);

For older browser check polyfill option of find method.


UPDATE : If you want to update all the element with that particular id then use Array#forEach method to iterate. Actually, there is no need of Array#find method since you just want to update the value.

$scope.replyList.forEach(function(v) {
   if(v.id == 85475) v.reply_content = 'dddddd';
});

var replyList = [{
  "id": "85475",
  "reply_content": "aaaaaaa",
  "reply_gender": "1",
  "reply_author": "John"
}, {
  "id": "85487",
  "reply_content": "bbbbbbb",
  "reply_gender": "1",
  "reply_author": "Ben"
}, {
  "id": "85504",
  "reply_content": "ccccccc",
  "reply_gender": "1",
  "reply_author": "Wang"
}];

replyList.forEach(function(v) {
  if (v.id == 85475) v.reply_content = 'dddddd';
});

console.log(replyList);

Solution 2

Use the .filter method of array and get the first position of the result

$scope.replyList.filter(function(item){
  return item.id === '85475';
})[0].reply_content = 'dddddd';

If exists more than one result with the given value of key, process them all using the .map method.

$scope.replyList
  .filter(function(item){
    return item.id === '85475';
  })
  .map(function(item){
     item.reply_content = 'ddddd'
     return item;
   });

Take advantage of the javascript functional approach!!

Share:
10,015

Related videos on Youtube

Benyi
Author by

Benyi

Hi, I'm Benyi, from Taiwan.

Updated on June 04, 2022

Comments

  • Benyi
    Benyi almost 2 years

    I'm using AngularJS to create a site.

    There's my array:

    $scope.replyList = [
      {
        "id": "85485",
        "reply_content": "aaaaaaa",
        "reply_gender": "1",
        "reply_author": "John"
      },
      {
        "id": "85487",
        "reply_content": "bbbbbbb",
        "reply_gender": "1",
        "reply_author": "Ben"
      },
      {
        "id": "85504",
        "reply_content": "ccccccc",
        "reply_gender": "1",
        "reply_author": "Wang"
      }
    ]
    

    What I want to do is update the item value by a given key id.

    For example, I'd like to update the content with id 85485. How can you deal with that?

    $scope.replyList[{id: 85475}].reply_content = 'dddddd'; /* failed */
    

    Thank you very much.

  • Benyi
    Benyi over 7 years
    Thank you so much! This code shows an error undefined is not an object. What is the question?
  • Pranav C Balan
    Pranav C Balan over 7 years
    @Benyi : there is no element with that id
  • Pranav C Balan
    Pranav C Balan over 7 years
    @Benyi : to avoid that keep a reference variable like : var obj = $scope.replyList.find(function(v) { return v.id == 85475; }); obj && obj.reply_content = 'dddddd';
  • Benyi
    Benyi over 7 years
    Thank you very much. I made a silly mistake. This works fine. It's helpful !
  • Pranav C Balan
    Pranav C Balan over 7 years
    @Benyi : glad to help
  • Benyi
    Benyi over 7 years
    Thank you very much. This code works fine with AngularJS!