JavaScript - Create Partial Objects Array from Another Array
Last Updated :
24 Jan, 2025
Here are the different ways to create an array of partial objects from another array in JavaScript
1. Using map()
The map() method is the most efficient and commonly used way to extract specific properties from objects in an array. It creates a new array by transforming each object.
JavaScript
const a = [
{ id: 1, age: 25, city: "Delhi" },
{ id: 2, age: 30, city: "Mumbai" },
{ id: 3, age: 35, city: "Chennai" },
];
const res = a.map(({ id, city }) =>
({ id, city }));
console.log(res);
Output[
{ id: 1, city: 'Delhi' },
{ id: 2, city: 'Mumbai' },
{ id: 3, city: 'Chennai' }
]
Here, object destructuring is used to extract only the id and city properties.
2. Using a for Loop
If you prefer more control over the process, a for loop is a simple way to achieve the same result.
JavaScript
const a = [
{ id: 1, marks: 85, state: "UP" },
{ id: 2, marks: 90, state: "MH" },
{ id: 3, marks: 80, state: "TN" },
];
const res = [];
for (let i = 0; i < a.length; i++) {
const { id, marks } = a[i];
res.push({ id, marks });
}
console.log(res);
Output[ { id: 1, marks: 85 }, { id: 2, marks: 90 }, { id: 3, marks: 80 } ]
This approach is useful when working in environments that don’t support modern ES6+ features.
3. Using reduce()
The reduce() method can also be used to transform an array into another array of partial objects. This is especially useful if you want to perform additional logic while extracting properties.
JavaScript
const a = [
{ id: 1, cost: 150, stock: 10 },
{ id: 2, cost: 200, stock: 5 },
{ id: 3, cost: 100, stock: 20 },
];
const res = a.reduce((acc, { id, cost }) => {
acc.push({ id, cost });
return acc;
}, []);
console.log(res);
Output[ { id: 1, cost: 150 }, { id: 2, cost: 200 }, { id: 3, cost: 100 } ]
This approach allows you to maintain full control over how the data is transformed.
4. Dynamically Selecting Properties
If the properties you want to extract are dynamic (e.g., based on user input), you can use an array of property names along with Object.fromEntries().
JavaScript
const a = [
{ id: 1, price: 500, qty: 10 },
{ id: 2, price: 700, qty: 15 },
{ id: 3, price: 300, qty: 20 },
];
const props = ["id", "price"];
const res = a.map((obj) =>
Object.fromEntries(props.map((key) =>
[key, obj[key]]))
);
console.log(res);
Output[ { id: 1, price: 500 }, { id: 2, price: 700 }, { id: 3, price: 300 } ]
This is ideal for scenarios where the required properties may change dynamically.
5. Extracting Multiple Partial Arrays
Sometimes, you might want to extract specific fields as separate arrays instead of partial objects.
JavaScript
const a = [
{ id: 1, category: "Electronics", brand: "Sony" },
{ id: 2, category: "Clothing", brand: "Zara" },
{ id: 3, category: "Furniture", brand: "IKEA" },
];
const ids = a.map((obj) => obj.id);
const brands = a.map((obj) => obj.brand);
console.log(ids);
console.log(brands);
Output[ 1, 2, 3 ]
[ 'Sony', 'Zara', 'IKEA' ]
This method is useful when you need arrays of specific fields for further processing.
6. Handling Nested Objects
If your objects contain nested structures, you can use destructuring with a combination of mapping techniques.
JavaScript
const a = [
{ id: 1, details: { price: 1000, stock: 50 } },
{ id: 2, details: { price: 1500, stock: 30 } },
{ id: 3, details: { price: 800, stock: 100 } },
];
const res = a.map(({ id, details: { price } }) =>
({ id, price }));
console.log(res);
Output[
{ id: 1, price: 1000 },
{ id: 2, price: 1500 },
{ id: 3, price: 800 }
]
This method is handy when working with deeply nested data.
Similar Reads
JavaScript - Create an Object From Two Arrays Here are the different methods to create an object from two arrays in JavaScript1. Using for-each loopThe arr.forEach() method calls the provided function once for each element of the array. JavaScriptconst a1 = ['name', 'age', 'city']; const a2 = ['Ajay', 25, 'New Delhi']; const res = {}; a1.forEac
3 min read
JavaScript - Create Array of Objects From Multiple Arrays Here are the different methods to create an array of objects from multiple arrays in JavaScript:1. Using map (Most Popular Approach)The map method is widely used because it is simple, concise, and works efficiently for arrays of the same length.JavaScriptconst a = ["apple", "banana", "orange"]; cons
3 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
JavaScript - Convert an Array to an Object These are the following ways to convert an array to an Object:1. Using JavaScript Object.assign() method The first approach is using the Object.assign() method. This method copies the values of all enumerable properties from source objects(one or more) to a target object.JavaScriptlet a = [1, 2, 3,
2 min read
Different Ways to Crate an Array of Objects in JavaScript ? Objects in JavaScript are key-value pairs, making them suitable for representing structured data. Also, an array in JavaScript is a versatile data structure that can hold a collection of values. When dealing with objects, an array can be used to store multiple objects. An array of objects allows you
3 min read
How to use forEach with an Array of Objects in JavaScript ? Using the forEach() method with an array of objects in JavaScript is essential for iterating over collections and performing operations on each object. This guide explores effective techniques to utilize forEach() for array manipulation, enhancing your coding skills. Syntax: array.forEach( function(
3 min read
JavaScript- Add an Object to JS Array In JavaScript, arrays are used to store multiple values in a single variable, and objects are collections of properties and values. Sometimes, we may need to add an object to an array to manage data more effectively. These are the following ways to add an object to a JS array: Using JavaScript Array
4 min read
How to Create an Array of Object Literals in a Loop using JavaScript? Creating arrays of object literals is a very frequent activity in JavaScript, as often when working with data structures, several objects are needed. This article will allow your hand through different ways of coming up with an array of object literals in a loop in JavaScript. Here are different app
5 min read
JavaScript - Convert Two-Dimensional Array Into an Object Here are the different methods to convert the two-dimensional array into an object in JavaScript.1. Using a for LoopThe simplest way to convert a two-dimensional array into an object is by iterating through the array using a for loop and assigning key-value pairs.JavaScriptconst a = [['name', 'Abhay
2 min read
How to convert a map to array of objects in JavaScript? A map in JavaScript is a set of unique key and value pairs that can hold multiple values with only a single occurrence. Sometimes, you may be required to convert a map into an array of objects that contains the key-value pairs of the map as the values of the object keys. Let us discuss some methods
6 min read