I have a flat JS object:
{a: 1, b: 2, c: 3, ..., z:26}
I want to clone the object except for one element:
{a: 1, c: 3, ..., z:26}
What's the easiest way to do this (preferring to use es6/7 if possible)?
I have a flat JS object:
{a: 1, b: 2, c: 3, ..., z:26}
I want to clone the object except for one element:
{a: 1, c: 3, ..., z:26}
What's the easiest way to do this (preferring to use es6/7 if possible)?
If you use Babel you can use the following syntax to copy property b from x into variable b and then copy rest of properties into variable y:
let x = {a: 1, b: 2, c: 3, z:26};
let {b, ...y} = x;
and it will be transpiled into:
"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 = x.b;
var y = _objectWithoutProperties(x, ["b"]);
var clone = Object.assign({}, {a: 1, b: 2, c: 3});
delete clone.b;
or if you accept property to be undefined:
var clone = Object.assign({}, {a: 1, b: 2, c: 3}, {b: undefined});
To add to Ilya Palkin's answer: you can even dynamically remove 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 in Babel REPL
Source:
For those who can't use ES6, you can use lodash
or underscore
.
_.omit(x, 'b')
Or ramda
.
R.omit('b', x)
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 it is faster than Object.assign and delete then: http://jsperf.com/omit-key
Maybe something like this:
var copy = Object.assign({}, {a: 1, b: 2, c: 3})
delete copy.c;
Is this good enough? Or can't c
actually get copied?
Hey seems like you run in to reference issues when you're trying to copy an object then deleting a property. Somewhere you have to assign primitive variables so javascript makes a new value.
Simple trick (may be horrendous) I used was 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"}
If you're dealing with a huge variable, you don't want to copy it and then delete it, as this would be inefficient.
A simple for-loop with a hasOwnProperty check should work, and it is much more adaptable to future needs :
for(var key in someObject) {
if(someObject.hasOwnProperty(key) && key != 'undesiredkey') {
copyOfObject[key] = someObject[key];
}
}
Lodash omit
let source = //{a: 1, b: 2, c: 3, ..., z:26}
let copySansProperty = _.omit(source, 'b');
// {a: 1, c: 3, ..., z:26}
You also can use 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 }
I use this one-liner ES6 syntax, the trick is to encapsulate the object in an array and call the map method to filter omit some properties. I like this method because it is easy to understand and you can easily add or remove properties.
const obj = {a: 1, b: 2, c: 3, d: 4};
const clone = [obj].map(({b, c, ...others}) => ({...others}))[0]; // remove b and c
console.log(clone);