最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to add new value inside array of object by id (not new item) - Stack Overflow

programmeradmin2浏览0评论

I'm trying add a new value inside my array by id. I'm not trying add a new item in my array... For this I can use push(), but it add new item not a new value.

I'm trying do it:

My array:

const data = 
[  
   {  
      "id": 1,
      "year":2019,
      "value": 2,
   },
   {  
      "id": 2,
      "year": 2019,
      "value": 89,
   },   
   {  
      "id": 3,
      "year": 2019,
      "value": 99,
   }   
]

Inside an especific id I would to add a new value like this:

data.forEach(item => {
  if(item.id === 2){
    //data inside id 2 -> item: 55
  }
})

So my new dataarray looks like this:

const data = 
[  
   {  
      "id": 1,
      "year":2019,
      "value": 2,
   },
   {  
      "id": 2,
      "year": 2019,
      "value": 89,
      "item": 55
   },   
   {  
      "id": 3,
      "year": 2019,
      "value": 99,
   }   
]

In most of my searches, I found just how to add a new element. But this I know how to do (push()).

So how to add a new value inside specified id?

I'm trying add a new value inside my array by id. I'm not trying add a new item in my array... For this I can use push(), but it add new item not a new value.

I'm trying do it:

My array:

const data = 
[  
   {  
      "id": 1,
      "year":2019,
      "value": 2,
   },
   {  
      "id": 2,
      "year": 2019,
      "value": 89,
   },   
   {  
      "id": 3,
      "year": 2019,
      "value": 99,
   }   
]

Inside an especific id I would to add a new value like this:

data.forEach(item => {
  if(item.id === 2){
    //data inside id 2 -> item: 55
  }
})

So my new dataarray looks like this:

const data = 
[  
   {  
      "id": 1,
      "year":2019,
      "value": 2,
   },
   {  
      "id": 2,
      "year": 2019,
      "value": 89,
      "item": 55
   },   
   {  
      "id": 3,
      "year": 2019,
      "value": 99,
   }   
]

In most of my searches, I found just how to add a new element. But this I know how to do (push()).

So how to add a new value inside specified id?

Share Improve this question edited May 7, 2019 at 19:57 Kamil Kiełczewski 92.9k34 gold badges395 silver badges370 bronze badges asked May 7, 2019 at 19:51 ZkkZkk 75115 silver badges31 bronze badges 0
Add a ment  | 

6 Answers 6

Reset to default 5

Just assign the property you want to add:

data.forEach(item => {
  if(item.id === 2){
    item.item = 55;
  }
})

If the IDs are unique, you can use the .find() method:

var el = data.find(item => item.id === 2);
if (el) {
  el.item = 55;
}

try

data.find(x=> x.id==2).item=55;

const data = 
[  
   {  
      "id": 1,
      "year":2019,
      "value": 2,
   },
   {  
      "id": 2,
      "year": 2019,
      "value": 89,
   },   
   {  
      "id": 3,
      "year": 2019,
      "value": 99,
   }   
]

data.find(x=>x.id==2).item=55;

console.log(data);

You can iterate and assign value based on your criteria

data.map(function(x){
  if(x.id == 2){
    x.value = 100;
  }
})

You can implement method using Array.find to avoid unnecessary iterations:

const array = [  
   {  
      "id": 1,
      "year":2019,
      "value": 2,
   },
   {  
      "id": 2,
      "year": 2019,
      "value": 89,
   },   
   {  
      "id": 3,
      "year": 2019,
      "value": 99,
   }   
];

const changeValue = (array, id, field, value) =>
  array.find(el => el.id === id)[field] = value;

changeValue(array, 1, 'year', 9999);
console.log('result: ', array);

You have an array of objects and you want to add a field to one of the objects. So, first, you have to find the object you want to change. Array items can be accessed by index, but you don't know the index. There are several methods to find an item in an array.

var item = data.find(function(d, i){
    return item.id === 2; //criteria
});

or in ES6 syntax:

var item = data.find(d=>d.id == 2);

after that, you can change item the way you want.

item.anotherField = 'another value';

As you said, push() adds an item to the array. It doesn't change existing items in the array.

Your code is more or less right there. To set the property of an item, you can do either object.propertyName = ... or object["propertyName"] = ....

With that, you'd simply need to update your example to look like this:

data.forEach(item => {
  if(item.id === 2){
    item.item = 55; //data inside id 2 -> item: 55
  }
})

As a more efficient alternative, consider Array.find(). It won't continue to loop through the array after it finds the id, whereas your forEach will always loop through the array in its entirety.

const data = [ { "id": 1, "year":2019, "value": 2, }, { "id": 2, "year": 2019, "value": 89, }, { "id": 3, "year": 2019, "value": 99, } ];

( data.find(({id})=> id === 2) || {} ).item = 55;
console.log(data);

You'll notice I've followed the .find() with || {}. This is simply so that if an item with id === 2 isn't found, attempting to set the property won't throw an error.

发布评论

评论列表(0)

  1. 暂无评论