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

0

3 Answers 3

42

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Why does setting it to an underscore remove it?
@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.
Is this possible if propertyName was an array? Trying to remove multiple computed properties from an object at once
@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.
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

2 Comments

Interestingly on Safari iOS my code runs faster. Cloning then deleteing is 43% slower.
@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
5
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',
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.