Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select all props but one from object [duplicate]

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.

like image 895
Ernane Luis Avatar asked Jan 25 '26 22:01

Ernane Luis


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)
like image 159
Ori Drori Avatar answered Jan 27 '26 12:01

Ori Drori