Clone js object except for one key

I have a flat JS object:

{a: 1, b: 2, c: 3, ..., z:26} 

I want to clone an object except for one element:

 {a: 1, c: 3, ..., z:26} 

What is the easiest way to do this (preferring to use es6 / 7 if possible)?

+223
javascript ecmascript-6 ecmascript-harmony ecmascript-7
Jan 09 '16 at 21:03
source share
17 answers

If you use Babel , you can use the following syntax to copy property b from x to variable b, and then copy the rest of the properties to variable y:

 let x = {a: 1, b: 2, c: 3, z:26}; let {b, ...y} = x; 

and it will be transferred to:

 "use strict"; function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; } var x = { a: 1, b: 2, c: 3, z: 26 }; var b = xb; var y = _objectWithoutProperties(x, ["b"]); 
+328
Jan 10 '16 at 19:54
source share
 var clone = Object.assign({}, {a: 1, b: 2, c: 3}); delete clone.b; 

or if you accept the undefined property:

 var clone = Object.assign({}, {a: 1, b: 2, c: 3}, {b: undefined}); 
+115
Jan 09 '16 at 21:07
source share

To add the answer to Ilya Palkin: you can even dynamically delete keys:

 const x = {a: 1, b: 2, c: 3, z:26}; const objectWithoutKey = (object, key) => { const {[key]: deletedKey, ...otherKeys} = object; return otherKeys; } console.log(objectWithoutKey(x, 'b')); // {a: 1, c: 3, z:26} console.log(x); // {a: 1, b: 2, c: 3, z:26}; 

Demo at Babel REPL

Source:

+56
Nov 20 '16 at 0:55
source share

For those who cannot use ES6, you can use lodash or underscore .

 _.omit(x, 'b') 

Or ramda .

 R.omit('b', x) 
+54
Jan 13 '17 at 4:16
source share

I use this single line ES6 syntax,

 const obj = {a: 1, b: 2, c: 3, d: 4}; const clone = (({b, c, ...others}) => ({...others}))(obj); // remove b and c console.log(clone); 
+29
Nov 23 '18 at 8:55
source share

You can write a simple helper function for it. Lodash has a similar function with the same name: omit

 function omit(obj, omitKey) { return Object.keys(obj).reduce((result, key) => { if(key !== omitKey) { result[key] = obj[key]; } return result; }, {}); } omit({a: 1, b: 2, c: 3}, 'c') // {a: 1, b: 2} 

Also note that this is faster than Object.assign and delete then: http://jsperf.com/omit-key

+20
Jan 10 '16 at 12:07 on
source share

Maybe something like this:

 var copy = Object.assign({}, {a: 1, b: 2, c: 3}) delete copy.c; 

Is this good enough? Or is it impossible c copy c ?

+9
Jan 09 '16 at 21:06
source share

Using Destruction

 const omit = (prop, { [prop]: _, ...rest }) => rest; const obj = { a: 1, b: 2, c: 3 }; const objWithoutA = omit('a', obj); console.log(objWithoutA); // {b: 2, c: 3} 
+7
Mar 23 '19 at 0:14
source share

Hi, it seems you encountered problems when trying to copy an object and delete a property. Somewhere you have to assign primitive variables for javascript to make a new value.

A simple trick (can be horrible), I used this

 var obj = {"key1":"value1","key2":"value2","key3":"value3"}; // assign it as a new variable for javascript to cache var copy = JSON.stringify(obj); // reconstitute as an object copy = JSON.parse(copy); // now you can safely run delete on the copy with completely new values delete copy.key2 console.log(obj) // output: {key1: "value1", key2: "value2", key3: "value3"} console.log(copy) // output: {key1: "value1", key3: "value3"} 
+6
Jun 07 '18 at 13:44 on
source share

Lodash lower

 let source = //{a: 1, b: 2, c: 3, ..., z:26} let copySansProperty = _.omit(source, 'b'); // {a: 1, c: 3, ..., z:26} 
+4
05 Oct '17 at 15:44
source share

If you are dealing with a huge variable, you do not want to copy it and then delete it, as that would be inefficient.

A simple check loop hasOwnProperty should work, and it is much more adapted to future needs:

 for(var key in someObject) { if(someObject.hasOwnProperty(key) && key != 'undesiredkey') { copyOfObject[key] = someObject[key]; } } 
+2
Aug 10 '17 at 13:40
source share

You can also use the spread operator to do this.

 const source = { a: 1, b: 2, c: 3, z: 26 } const copy = { ...source, ...{ b: undefined } } // { a: 1, c: 3, z: 26 } 
+2
Sep 12 '18 at 10:15
source share

I recently did this in a very simple way:

 const obj = {a: 1, b: 2, ..., z:26}; 

just using the distribution operator to separate the unwanted property:

 const {b, ...rest} = obj; 

... and object.assign to take only part of the rest:

 const newObj = Object.assign({}, {...rest}); 
+2
Feb 27 '19 at 10:26
source share

How about this? I never met this pattern, but I was just trying to exclude one or more properties without the need to create an additional object. This seems to do the job, but there are some side effects that I cannot see. Surely not very readable.

 const postData = { token: 'secret-token', publicKey: 'public is safe', somethingElse: true, }; const a = { ...(({token, ...rest} = postData) => (rest))(), } /** a: { publicKey: 'public is safe', somethingElse: true, } */ 
+2
Mar 19 '19 at 12:08
source share

The solutions described above using structuring suffer from the fact that you have a variable that can cause complaints from ESLint if you use it.

So here are my solutions:

 const src = { a: 1, b: 2 } const result = Object.keys(src) .reduce((acc, k) => k === 'b' ? acc : { ...acc, [k]: src[k] }, {}) 

On most platforms (except IE, if Babel is not used), you can also do:

 const src = { a: 1, b: 2 } const result = Object.fromEntries( Object.entries(src).filter(k => k !== 'b')) 
+2
Jun 06 '19 at 17:15
source share

I did it this way, as an example from my Redux reducer:

  const clone = { ...state }; delete clone[action.id]; return clone; 

In other words:

 const clone = { ...originalObject } // note: original object is not altered delete clone[unwantedKey] // or use clone.unwantedKey or any other applicable syntax return clone // the original object without the unwanted key 
+1
Apr 28 '19 at 22:31
source share

This should also do this, just delete this key:

 var copy = (obj, del)=>{ delete obj.del; return obj; } 
-one
Jul 28 '19 at 15:11
source share



All Articles