I have an array of objects as shown below
JavaScript
x
10
10
1
Object {Results:Array[2]}
2
Results:Array[2]
3
[0-1]
4
0:Object
5
id=1
6
name: "Rick"
7
1:Object
8
id=2
9
name:'david'
10
I want to add one more property named Active to each element of this array of Objects.
The final outcome should be as follows.
JavaScript
1
12
12
1
Object {Results:Array[2]}
2
Results:Array[2]
3
[0-1]
4
0:Object
5
id=1
6
name: "Rick"
7
Active: "false"
8
1:Object
9
id=2
10
name:'david'
11
Active: "false"
12
Can someone please let me know how to achieve this.
Advertisement
Answer
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.
JavaScript
1
4
1
Results.forEach(function (element) {
2
element.Active = "false";
3
});
4