Use Array.prototype.map()
Results.map(obj => ({ ...obj, Active: 'false' }))
Read the documentation for more information.
Answer from sidonaldson on Stack OverflowUse Array.prototype.map()
Results.map(obj => ({ ...obj, Active: 'false' }))
Read the documentation for more information.
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";
});
Is there a way to set a new property to all objects in an array of objects without looping?
Add property to all objects in array - javascript
angular - How to add property to each object of an array in typescript.? - Stack Overflow
Add property to each object in the array
you can use array.map,
and you should use Number() to convert props to numbers for adding:
var array = [ {'a': '12', 'b':'10'}, {'a': '20', 'b':'22'} ];
var r = array.map( x => {
x.c = Number(x.b) - Number(x.a);
return x
})
console.log(r)
And, with the support of the spread operator, a more functional approach would be:
array.map(x => ({
...x,
c: Number(x.a) - Number(x.b)
}))
Use forEach function:
var array = [{ 'a': '12', 'b': '10' }, { 'a': '20', 'b': '22' }];
array.forEach(e => e.c = +e.b - +e.a);
console.log(JSON.stringify(array));
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.
Array.push adds the element to the end of the array, so you would have to access it via people[people.length-1].total
You have to use forEach at this context,
capitals.forEach(function(itm){
itm.global = true;
});
If you are not familiar with the usage of forEach, then you can do the same job by using a for loop.
for(var i=0,len=capitals.length;i<len;i++){
capitals[i].global = true;
}
If you do not have to support legacy browsers, then you could use Array.prototype.map
You can use Array.map method to get the result. Here's code
var object = [
{account:2423, user:1564},
{account:1235, user:1564}
];
object.map(function(entry) {
entry.username = "clientname";
return entry;
});
Iterate over elements using Array#forEach and set username property.
var object = [{
account: 2423,
user: 1564
}, {
account: 1235,
user: 1564
}];
object.forEach(function(v) {
v.username = 'clientname';
})
console.log(object);
Even you can use the simple for loop.
var object = [{
account: 2423,
user: 1564
}, {
account: 1235,
user: 1564
}];
for (var i = 0; i < object.length; i++) {
object[i].username = 'clientname';
}
console.log(object);
FYI : Without loop it may be more complex and inefficient. If limited numbers of elements are there then you can update it with its index although as @squint suggested recursion can be used but I don't think all those are necessary here since it's a job of a simple loop .
I need help adding a property with the value i to each object during a loop.
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];