3

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.

Ernane Luis
  • 353
  • 2
  • 9

1 Answers1

2

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)
Ori Drori
  • 183,571
  • 29
  • 224
  • 209