JavaScript Program to Extract Given Property Values from Objects as Array

To understand this example, you should have the knowledge of the following JavaScript programming topics:


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 for loop is used to iterate through all the elements of an array.
  • During each iteration, the value of property a is pushed to the extractedValue array.