How to extract value of a property as array from an array of objects ?
Last Updated :
02 Jul, 2024
We will try to understand that how we may extract the value of a property as an array from an array of objects in JavaScript with the help of certain examples.
Pre-requisite: Array of Objects in JavaScript
Example:
Input:
[
{
apple: 2,
mango: 4,
},
{
apple: 3,
mango: 6,
},
{
apple: 7,
mango: 11,
},
]
Output: [2, 3, 7]
Explanation:
- From an array containing multiple objects, we first have to select on our own the desired key.
- Thereafter choosing the desired key we will have to select all the values corresponding to the key in each and every object passed inside the array.
- Thereafter in output, we have to print the values corresponding to the key in the form of an array.
After understanding the desired task from the above example, let us quickly see the following approaches through which we may understand as well as solve solutions for the above-shown task.
Approach 1: Using Array Push
- A helper function is created which consists of a parameter that is our main array of objects.
- Create a temporary array inside the function in which results will be stored and printed later.
- Then using the for-of loop, iterate over the values of that array of objects and thereafter select values for the desired chosen key.
- Later after fetching values store those values in that temporary array, return that value from the function, and then print it as an output.
Example: Below is the implementation of the above-illustrated approach.
JavaScript
let desiredValue = (fruits_quantity) => {
let output = [];
for (let item of fruits_quantity) {
output.push(item.apple);
}
return output;
};
let fruits_quantity = [
{
apple: 2,
mango: 4,
},
{
apple: 3,
mango: 6,
},
{
apple: 7,
mango: 11,
},
];
let result = desiredValue(fruits_quantity);
console.log(result);
Output:
[ 2, 3, 7 ]
Approach 2: Using Map
- A helper function is created which consists of two parameters: the first one is our main array of objects and the second parameter is our desired key name.
- As illustrated in the previous approach, again iterate over the array of objects, but this time map() function of the array is used.
- Thereafter value in the form of an array is returned.
Example: Below is the implementation of the above approach.
JavaScript
let desiredValue = (fruits_quantity, desired_key) => {
let desiredValue = fruits_quantity.map((element) => element[desired_key]);
return desiredValue;
};
let fruits_quantity = [
{
apple: 2,
mango: 4,
},
{
apple: 3,
mango: 6,
},
{
apple: 7,
mango: 11,
},
];
// Corresponding to this key values
// (as an fruits_quantity) would be obtained...
let desired_key = "apple";
let result = desiredValue(fruits_quantity, desired_key);
console.log(result);
Output:
[ 2, 3, 7 ]
Approach 3: Using reduce()
The `reduce()` method can be used to extract a property into an array by accumulating values in an initial array. Iterate over each object, push the desired property into the accumulator, and return it. This method is concise and functional for such transformations.
Example:
JavaScript
const objectsArray = [
{ id: 1, name: 'Alice' },
{ id: 2, name: 'Bob' },
{ id: 3, name: 'Charlie' }
];
const names = objectsArray.reduce((acc, obj) => {
acc.push(obj.name);
return acc;
}, []);
console.log(names); // ['Alice', 'Bob', 'Charlie']
Output[ 'Alice', 'Bob', 'Charlie' ]
Approach 4: Using for...of Loop
Using the for...of loop to extract property values involves iterating over the array of objects and pushing each object's property value into a new array. This method is straightforward and works well for simple extraction tasks.
Example
JavaScript
let objectsArray = [{name: 'Alice'}, {name: 'Bob'}, {name: 'Charlie'}];
let names = [];
for (let obj of objectsArray) {
names.push(obj.name);
}
console.log(names);
Output[ 'Alice', 'Bob', 'Charlie' ]
Similar Reads
How to Convert an Array of Objects into an Array of Arrays ? An Array of objects is an array that contains multiple objects as its elements which can be converted into an array of arrays i.e. an array that contains multiple arrays as its elements. There are several methods listed below to achieve this task. Table of Content Using Object.keys() and Object.valu
3 min read
How to get the Index of an Array based on a Property Value in TypeScript ? Getting the index of an array based on a property value involves finding the position of an object within the array where a specific property matches a given value. The below approaches can be used to accomplish the task. Table of Content Using Array.findIndex()Using reduce() methodUsing for loopUsi
4 min read
How to Replace an Object in an Array with Another Object Based on Property ? In JavaScript, an array is a data structure that can hold a collection of values, which can be of any data type, including numbers, strings, and objects. When an array contains objects, it is called an array of objects. Table of Content Using findIndex and splice methodsUsing filter and concat metho
3 min read
How to find property values from an array containing multiple objects in JavaScript ? To find property values from an array containing multiple objects in JavaScript, we have multiple approaches. In this article, we will learn how to find property values from an array containing multiple objects in JavaScript. Several methods can be used to find property values from an array containi
4 min read
How to Remove a Property from All Objects in an Array in JavaScript? To remove a property from all objects in an array in JavaScript, you can use the forEach() method to iterate over each object in the array and use the delete operator to remove the specified property.Example:JavaScriptconst arrayOfObjects = [ { name: "Alice", age: 25, city: "New York" }, { name: "Bo
2 min read
How to Create Array of Objects From Keys & Values of Another Object in JavaScript ? Creating an array of objects from the keys and values of another object involves transforming the key-value pairs of the original object into individual objects within an array. Each object in the array represents a key-value pair from the source object. Below approaches can be used to accomplish th
3 min read