Add property to an array of objects

JavascriptArraysObjectunderscore.js

Javascript Problem Overview


I have an array of objects as shown below

Object {Results:Array[2]}
     Results:Array[2]
[0-1]
0:Object
       id=1     
       name: "Rick"
1:Object
       id=2     
       name:'david'

I want to add one more property named Active to each element of this array of Objects.

The final outcome should be as follows.

Object {Results:Array[2]}
     Results:Array[2]
[0-1]
0:Object
       id=1     
       name: "Rick"
       Active: "false"
1:Object
       id=2     
       name:'david'
       Active: "false"

Can someone please let me know how to achieve this.

Javascript Solutions


Solution 1 - Javascript

Use Array.prototype.map()

Results.map(obj => ({ ...obj, Active: 'false' }))

Read the documentation for more information.

Solution 2 - Javascript

You can use the forEach method to execute a provided function once for each element in the array. In this provided function you can add the Active property to the element.

Results.forEach(function (element) {
  element.Active = "false";
});

Solution 3 - Javascript

With ES6 you can simply do:

 for(const element of Results) {
      element.Active = "false";
 }

Solution 4 - Javascript

I came up against this problem too, and in trying to solve it I kept crashing the chrome tab that was running my app. It looks like the spread operator for objects was the culprit.

With a little help from adrianolsk’s comment and sidonaldson's answer above, I used Object.assign() the output of the spread operator from babel, like so:

this.options.map(option => {
  // New properties to be added
  const newPropsObj = {
    newkey1:value1,
    newkey2:value2
  };

  // Assign new properties and return
  return Object.assign(option, newPropsObj);
});

Solution 5 - Javascript

  Object.defineProperty(Results, "Active", {value : 'true',
                       writable : true,
                       enumerable : true,
                       configurable : true});

Solution 6 - Javascript

It goes through the object as a key-value structure. Then it will add a new property named 'Active' and a sample value for this property ('Active) to every single object inside of this object. this code can be applied for both array of objects and object of objects.

   Object.keys(Results).forEach(function (key){
            Object.defineProperty(Results[key], "Active", { value: "the appropriate value"});
        });

Solution 7 - Javascript

You can use map or foreach to do that. There are many answers with foreach so I will be telling about how to do it with map.

  let new_array=array.map(function(ele){
       
       return {...ele,Active:'false'};
     })

If you dont want to have a new array and instead mutate it you can use foreach.But if you want to mutate it with map as you are stubborn like me,you can do that by:

array.map(function(ele){
       
       return ele.Active='false';
     });

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPatrickView Question on Stackoverflow
Solution 1 - JavascriptsidonaldsonView Answer on Stackoverflow
Solution 2 - JavascriptTholleView Answer on Stackoverflow
Solution 3 - JavascriptSalim LyoussiView Answer on Stackoverflow
Solution 4 - JavascriptJoel BalmerView Answer on Stackoverflow
Solution 5 - JavascriptIvan De Jesus Quezada SeguraView Answer on Stackoverflow
Solution 6 - JavascriptArash MASView Answer on Stackoverflow
Solution 7 - JavascriptAmar ShuklaView Answer on Stackoverflow