Optional keys in objects

Yep, but there is a corner stones here. Some might want to remove undefined and null, other NaN or Infinity. Method to delete properties could solve all of this.

Object.clean({a: undefined, b: null}) // => {}
Object.clean({a: false, b: null}, (v) => v !== false) // => {b: null}

// Method modifies target object
let value = {a: undefined}
'a' in value // => true
Object.clean(value)
'a' in value // => false

Usage

function enrich(input, options) {
  return Object.clean({
     ...input, additionalKey: options,
  })
}

And syntax for this should be part of object restructuring syntax proposal, which should be more powerful and cover more object manipulation cases.

1 Like