How to select all props but one from object

Viewed 438

I wanna to select all props but one from a javascript object but doing in elegant way using ES6, is this possible?

example:

const myObj = { prop1, prop2, prop3 }
const newObj = {
…myObject.remove(prop3)
}

then newObj should be { prop1, prop2}

if destruct I can select some, or all

const newObj = {
…myObject
}

const {prop1, prop2} = myObjct

but I dont know how to select all but one.

1 Answers

You can use the object rest syntax to assign all other properties to newObj, except those explicitly stated:

const myObj = { prop1: 1, prop2: 2, prop3: 3 }

const { prop1, ...newObj } = myObj

console.log(newObj)

Related