21

I have an object as follows :

let obj = {foo: 1, bar: 2, baz: 3}

I would like to delete a specific property by calling a method that takes as parameter the name of the property to delete

removeProperty(obj, propertyName) {
  let { propertyName, _, ...result } = obj
  return result
}

the problem is that this method only works when we write the name of the property directly in the syntax spead, like: let { bar, _, ...result } = obj .But it does not work by passing it as a parameter, because the syntax spead creates it as a new variable

how can we do that, with another solution if possible except the omit of lodash

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Mouad Ennaciri
  • 1,217
  • 3
  • 15
  • 28

3 Answers3

41

You can use computed properties in destructuring:

let obj = {foo: 1, bar: 2, baz: 3}
function removeProperty(obj, propertyName) {
  let { [propertyName]: _, ...result } = obj
  return result
}
console.log(removeProperty(obj, 'foo'));

This will assign the property with the name of the value propertyName to a throwaway variable and essentially remove that key. See the MDN documentation.

Andrew Li
  • 55,805
  • 14
  • 125
  • 143
  • Why does setting it to an underscore remove it? – Symphony0084 Feb 03 '20 at 15:04
  • @manley13 It doesn't need to be. The underscore could be anything. It's just convention to use an underscore as a dummy variable in programming since it's not used. – Andrew Li Feb 03 '20 at 15:24
  • Is this possible if propertyName was an array? Trying to remove multiple computed properties from an object at once – Marc Sloth Eastman Feb 13 '20 at 21:00
  • @MarcSlothEastman Not that I know of. You can use a loop and `delete` like in the other answer or filter out keys: `Object.keys(obj).filter(key => !forbiddenKeys.includes(key)).reduce((acc, key) => ({ ...acc, [key]: obj[key] }), {})` though it's quite a sore on the eyes. It'd be more clear to do the former. – Andrew Li Feb 13 '20 at 22:11
10

Another alternative to destructuring would be to use delete. The following solution reduces time-complexity by about 35% compared to destructuring (in Desktop Chrome)

Solution

let obj = {foo: 1, bar: 2, baz: 3}
function removeProperty(obj, propertyName) {
  let newObj = {...obj};
  delete newObj[propertyName];
  return newObj;
}
console.log(removeProperty(obj, 'foo'));

Performance Test

https://jsperf.com/so53753276

The results vary depending upon the browser used. The results are rather intriguing. Desktop Safari destructuring outperforms delete, but Desktop Chrome out performs all numbers from Desktop Safari.

+-----------------------------------+
| Browser | delete    | destructure |
+---------+-----------+-------------+
| Chrome  | 3,229,791 | 1,993,256   |
| Safari  | 1,186,679 | 1,872,396   | 
+---------+-----------+-------------+

The results on iOS are less surprising, as Chrome is just really Safari under the hood.

+-----------------------------------+
| Browser | delete    | destructure |
+---------+-----------+-------------+
| Chrome  | 1,146,496 | 1,785,551   |
| Safari  | 1,182,067 | 1,793,772   | 
+---------+-----------+-------------+

Documentation

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete

AnonymousSB
  • 3,516
  • 10
  • 28
  • Interestingly on Safari iOS my code runs faster. Cloning then `delete`ing is 43% slower. – Andrew Li Dec 13 '18 at 02:13
  • @Li357 It does indeed appear that your solution is the most performant overall when weighted against all browsers; however, Desktop Chrome does shockingly outperform everyone when it comes to `delete` – AnonymousSB Dec 13 '18 at 04:07
4
values = {
    id: 1,
    name: 'hello world',
    vehicle: 'car',
    time: '3.30 pm',
    date: '02 MAR 1990',
};

const { time, date, ...rest } = values;

now ***...rest*** contains the object values of {
    id: 1,
    name: 'hello world',
    vehicle: 'car',
};