Use Array.prototype.map()

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

Read the documentation for more information.

Answer from sidonaldson on Stack Overflow
🌐
JavaScript in Plain English
javascript.plainenglish.io › how-to-add-a-property-to-each-object-in-an-array-of-objects-with-javascript-190736297065
How to Add a Property to Each Object in an Array of Objects with JavaScript | by John Au-Yeung | JavaScript in Plain English
March 27, 2023 - We can use the forEach method to loop through each element in an object and add a property to each. ... const arr = [{ a: 'foo' }, { a: 'bar' }, { a: 'baz' }] arr.forEach((element) => { element.b = 'qux' }); console.log(arr) We have the arr array.
Discussions

Is there a way to set a new property to all objects in an array of objects without looping?
Andrew Alvarez is having issues with: I've been practicing with arrays in Javascript, and I created an issue for myself to try to solve. I have an array of objects that I created: More on teamtreehouse.com
🌐 teamtreehouse.com
1
April 5, 2021
Add property to all objects in array - javascript
Communities for your favorite technologies. Explore all Collectives · Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work More on stackoverflow.com
🌐 stackoverflow.com
angular - How to add property to each object of an array in typescript.? - Stack Overflow
i need to add property to each object an array . i have searched a lot , got this, but this is AngularJs. I have tried below but not works. Any help appreciated export class ThreeComponent imple... More on stackoverflow.com
🌐 stackoverflow.com
Add property to each object in the array
I'm trying to add a property to each object in the array: More on stackoverflow.com
🌐 stackoverflow.com
April 6, 2016
🌐
ProgrammingBasic
programmingbasic.com › home › javascript › add property to each object in an array of object in javascript
Add property to each object in an array of object in JavaScript | ProgrammingBasic
In the above example, we have an array of objects with name property and we have looped through each object in the array with forEach() method and added the city property to it using dot notation.
🌐
Team Treehouse
teamtreehouse.com › community › is-there-a-way-to-set-a-new-property-to-all-objects-in-an-array-of-objects-without-looping
Is there a way to set a new property to all objects in an array of objects without looping? (Example) | Treehouse Community
April 5, 2021 - I have an array of objects that I created: const fruit = [ { name : 'Apple', price : 1.50, quantity : 17, }, { name : 'Banana', price : 2.50, quantity : 21, }, { name : 'Pear', price : 4.50, quantity : 11, }, { name : 'Strawberry', price : 5.50, quantity : 7, }, ]; I would like to have a total inventory value for all available stock. I need to add a new property to each object that multiplies quantity and price.
🌐
GitHub
github.com › Automedon › CodeWars-7-kyu-Soluitions › blob › master › Add property to every object in array
CodeWars-7-kyu-Soluitions/Add property to every object in array at master · Automedon/CodeWars-7-kyu-Soluitions
CodeWars 7 kyu Solutions (Please leave a star thank you) Created by https://github.com/Automedon - CodeWars-7-kyu-Soluitions/Add property to every object in array at master · Automedon/CodeWars-7-kyu-Soluitions
Author: Automedon
🌐
xjavascript
xjavascript.com › blog › add-property-to-an-array-of-objects
How to Add a Property to Each Object in an Array of Objects (JavaScript Example) — xjavascript.com
It is ideal when you want to avoid ... is critical). To add a property without mutation, return a new object (copy existing properties + add new ones) for each element....
🌐
JSchallenger
jschallenger.com › javascript-practice › javascript-objects › add-property-array-of-objects
Add property to each object in array | JSchallenger
JSchallenger. Write a function that takes an array of objects and a string as arguments. Add a property with key 'continent' and value equal to the string to each of the objects. Return the new array of objects. Hint: try not to mutate the original array
Find elsewhere
Top answer
1 of 2
32

NEW ANSWER

You changed your question completely, you should probably make a new question instead of changing the whole concept. Anyway, if you have to add new properties to your data objects, there is high chance that your app is designed wrong.

To add new property you don't use .push() because this is array method, instead you wanted to add new property to all objects. You can do this by using loop for example like this:

for (var i = 0; i < this.people.length; i++) {
    this.people[i].total = 2; // Add "total": 2 to all objects in array
}

or array .map()

this.people.map((obj) => {
    obj.total = 2;
    // or via brackets
    // obj['total'] = 2;
    return obj;
})

Also, if you need to merge objects or add more unknown properties you can use loop or Object.assign();

for (var i = 0; i < this.people.length; i++) {
    // merge objects into one with multiple props
    this.people[i] = Object.assign(this.people[i], {
        total: '2', 
        someProp: 'hello', 
        likePizza: true, 
    });
}

OLD ANSWER

Ecma5 is compatabile for ES6 and looks like you probably don't know what you want to do.

You put your code in ngOnInit so be sure you call your console.log after this event. Your code says you called console.log(people[1].total) outside of your component class so it can't even access this property.

Also you should not mix different types of objects in one array - this is why typescript was made, to avoid having different things in arrays and objects. Later in loop calling element[i].product can casue error becasue your new object has no such property.

export class ThreeComponent implements OnInit {
  people = [];
  // people: Array<YourObjects>; // would be better

 ngOnInit() { 
   this.people = [
     {'name': 'person 1', 'product': 'Medicine 1'},
     {'name': 'person 2', 'product': 'Medicine 2'},
     {'name': 'person 3', 'product': 'Medicine 3'},
   ];
   let newLength = this.people.push({'total' : '2'}); // returns new array length
   console.log(this.people[newLength ].total); // it works in this case
 }

}

.push() returns new array length what is your new element index in this case becasue push adds new elements at the end of array.

2 of 2
3

Array.push adds the element to the end of the array, so you would have to access it via people[people.length-1].total

🌐
TutorialsPoint
tutorialspoint.com › add-property-to-common-items-in-array-and-array-of-objects-javascript
Add property to common items in array and array of objects - JavaScript?
October 3, 2020 - To add property, use map(). Let’s say the following is our array − const firstname = ['John', 'David', 'Bob']; Following are our array of objects −
🌐
The Web Dev
thewebdev.info › home › how to add a property to each object in an array of objects with javascript?
How to Add a Property to Each Object in an Array of Objects with JavaScript? - The Web Dev
September 2, 2022 - We can use the forEach method to loop through each element in an object and add a property to each. ... const arr = [{ a: 'foo' }, { a: 'bar' }, { a: 'baz' }] arr.forEach((element) => { element.b = 'qux' }); console.log(arr) We have the arr array.
🌐
GitHub
gist.github.com › khanhhuy288 › c3be327301f4ff48dfee1dc0b4f063d7
Add property to every object in array · GitHub
Clone this repository at &lt;script src=&quot;https://gist.github.com/khanhhuy288/c3be327301f4ff48dfee1dc0b4f063d7.js&quot;&gt;&lt;/script&gt; Save khanhhuy288/c3be327301f4ff48dfee1dc0b4f063d7 to your computer and use it in GitHub Desktop. Download ZIP · Add property to every object in array ·
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-modify-an-objects-property-in-an-array-of-objects-in-javascript
JavaScript - Modify Object's Property in an Array of Objects - GeeksforGeeks
When the object with the name "Ajay" is found, its age property is updated to 26. The forEach() method in JavaScript is used to iterate over an array and execute a provided function on each element.
Published: December 4, 2025
🌐
Stack Overflow
stackoverflow.com › questions › 47092502 › javascript-adding-a-property-to-all-objects-in-an-array
javascript adding a property to all objects in an array - Stack Overflow
@LucaDeNardi all the solutions there are looping through the array and adding the property. This is precisely what i want to avoid. ... Those are the only ways to do that. The fact that an array is a list of multiple items require you to loop through all the items ... If you want to avoid looping you can try something like JSON.parse(JSON.stringify([{prop: 'x'}, {prop: 'y'}, {prop: 'z'}]).replace(/"prop"/,'"xyz"')); but I still think looping is the proper way
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-modify-an-objects-property-in-an-array-of-objects-in-javascript
How to modify an object’s property in an array of objects in JavaScript ? | GeeksforGeeks
The function modifyProperty updates the emp_name of an employee with a specific emp_id in the emp_data array. It uses forEach to find the matching emp_id and changes the emp_name to the new value. Using find() to search for the object with the specified property value, and destructuring to modify the property.
Published: January 7, 2025
🌐
Reddit
reddit.com › r/nodered › adding property to object during loop
r/nodered on Reddit: Adding property to object during loop
September 14, 2022 -

I need help adding a property with the value i to each object during a loop.

The current loop. I need the i-value to be added as a value to a new property in each object.

I'm new to JS, and really I'm just stumbling my way through failure after failure untill something sticks. This, however I can't seem to figure out.

The current loop function in text:

var msgList = [];
var objects = msg.payload;
for (var i = 0; i < msg.obj.length; i++){
    msgList.push({payload:objects[Object.keys(objects)[i]]});
}


return [msgList];