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.
🌐
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
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

Find elsewhere
🌐
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.
🌐
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 −
🌐
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
🌐
YouTube
youtube.com › watch
How to dynamically add properties in a JavaScript object array - YouTube
This tutorial explains - How to dynamically add properties in a JavaScript object array
Published: May 9, 2018
🌐
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