-2

how to remove key and value that are undefined from object?

for example I have

var x = {
 firstname: undefined,
 lastname: 'blabla'
}

how to get object without undefined? in the most efficient and quick way?

the result should be:

x = {
 lastname: 'blabla'
}
Jon Sud
  • 10,211
  • 17
  • 76
  • 174
  • Have you tried anything? – Etheryte May 22 '19 at 14:55
  • 1
    Possible duplicate of [Javascript - removing undefined fields from an object](https://stackoverflow.com/questions/25421233/javascript-removing-undefined-fields-from-an-object) and [Remove blank attributes from an Object in Javascript](https://stackoverflow.com/questions/286141) – adiga May 22 '19 at 15:02

1 Answers1

1
var x = {
 firstname: undefined,
 lastname: 'blabla'
}

for (let key in x) {
  if (x[key] === undefined) {
   delete x[key];
  }
}


Muhammad Usman
  • 458
  • 3
  • 9