Skip to content

Write me a function that will remove all undefined and null values from a JS object #1

Description

@indiesquidge

Given this JS object

const george = {
  name: 'George Costanza',
  age: 37,
  skills: undefined,
  vocation: null
}

Write me a function that will return a new object (no mutation) with any null or undefined values excluded.

const newGeorge = removeNil(obj)
console.log(newGeorge) // { name: 'George Costanza', age: 37 }

Add in the ability for me to include source objects, similar to the ES6 Object.assign method.

const art = removeNil(obj, { name: 'Art Vandelay', age: null, vocation: 'architect' })
console.log(art) // { name: 'Art Vandelay', vocation: 'architect' }

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions