Search code examples
javascriptarraysjavascript-objects

How do I create an array of partial objects from another array?


I have an array of objects, and I want to copy the array, but not include all the properties of the objects in the new array. Is there a better way to do that than this?

let objects = [{a:1,b:2,c:3},{a:4,b:5,c:6},{a:7,b:8,c:9}]

let partialObjects = objects.map(object => {
    let { a, ...partial } = object;
    return partial;
});

Desired Output:

[{b:2, c:3},{b:5, c:6},{b: 8, c:9}]

Solution

  • You can use destructuring directly in the arrow function in case you want more concise syntax:

    let objects = [{a:1,b:2,c:3},{a:4,b:5,c:6},{a:7,b:8,c:9}]
    
    let partialObjects = objects.map(({a,...rest})=> rest);
    console.log(partialObjects);