Example 1: Extract Value Using map()
Output
[1, 4, 8]
In the above program, the property's value of key a is extracted from each object of an array.
The map() method is used to iterate through all the elements of an array and extract property values of key a.
Note: You could also use a regular function instead of an arrow function.
let extractedValue = arr.map(function(item) {return item[prop]});
Example 2: Extract Value Using for Loop
Output
[1, 4, 8]
In the above program, the property value of key a is extracted from each object of an array.
- Initially, the extractedValue array is empty.
- The
forloop is used to iterate through all the elements of an array. - During each iteration, the value of property
ais pushed to the extractedValue array.