remove property from object javascript spread operator code example

Example 1: remove a object name from spread operator

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'));

Example 2: remove a object name from spread operator

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

Example 3: remove a object name from spread operator

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

Example 4: destructure object exlude one

const myObject = {  a: 1,  b: 2,  c: 3};const { a, ...noA } = myObject;console.log(noA); // => { b: 2, c: 3 }

Tags:

Misc Example