0

I have this object:

  this.prepaidObject = {
            'customerType' : this.prepaidDetailForm.prepaidDetails.customerType,
            'firstName' : this.prepaidDetailForm.prepaidDetails.firstName,
            'lastName' : this.prepaidDetailForm.prepaidDetails.lastName,
            'note' : this.prepaidDetailForm.prepaidDetails.note,
            'created': this.prepaidDetailForm.prepaidDetails.created
        };

Now sometimes some of this property are undefined. What i want is if one of this.prepaidDetailForm.prepaidDetails properties are undefined not to display them. So for example if this.prepaidDetailForm.prepaidDetails.firsName is undefined i dont need to create 'firstName' property isnide object. Any suggestion how can i do that?

eko
  • 39,722
  • 10
  • 72
  • 98
None
  • 8,817
  • 26
  • 96
  • 171

8 Answers8

6

Check your object:

    for( var m in this.prepaidObject ) {
        if ( this.prepaidObject[m] == undefined ) {
            delete this.prepaidObject[m];
        }
    } 
SPlatten
  • 5,334
  • 11
  • 57
  • 128
5

The shortest way to do this is to parse it after stringify.

The exact syntax:

let prepaidObjectNew = JSON.parse(JSON.stringify(prepaidObject))

Example

Before:

var prepaidObject = {
    'customerType' : 'TestCustType',
    'firstName' : "sample FirstName",
    'lastName' : "sample LastName",
    'note' : undefined
 };

After:

{
    'customerType' : 'TestCustType',
    'firstName' : "sample FirstName",
    'lastName' : "sample LastName"
 };
Mostafiz Rahman
  • 8,169
  • 7
  • 57
  • 74
1

One way is to go through the keys and only append the defined ones:

this.prepaidObject = { };
Object.keys(this.prepaidDetailForm.prepaidDetails)
     .forEach(function(key) {
           var val = this.prepaidDetailForm.prepaidDetails[key];
           if (val !== undefined) {
                this.prepaidObject[key] = val;
           }
     });

This is assuming the keys in prepaidObject are the same as in prepaidDetails and all of them follow the same rule you mention.

Especially if you're using ES6 you might make this more elegant using map and reduce like so:

this.prepaidObject = Object.keys(this.prepaidDetailForm.prepaidDetails)
     .map(key => ({key, val: this.prepaidDetailForm.prepaidDetails[key]}))
     .reduce((obj, {key, val}) => {
          if (val !== undefined) {
               obj[key] = val;
          }
          return obj;
     }, {});

And an even more succinct approach using more ES6 features:

this.prepaidObject = Object.keys(this.prepaidDetailForm.prepaidDetails)
     .map(key => ({key, val: this.prepaidDetailForm.prepaidDetails[key]}))
     .filter(({_, val}) => val !== undefined)
     .reduce((obj, {key, val}) => Object.assign(obj, { [key]: val }), {});
Ovidiu Dolha
  • 5,335
  • 1
  • 21
  • 30
1

Using Lodash make it too easy in one line:

_.pickBy(this.prepaidObject, _.identity);

That would remove all falsey values

SeleM
  • 9,310
  • 5
  • 32
  • 51
0

You probably need to check the type.

(typeof(this.prepaidDetailForm.prepaidDetails.firsName) != "undefined") ? this.prepaidDetailForm.prepaidDetails.firsName : ''

Its basically:

var firstName = '';
if(typeof(this.prepaidDetailForm.prepaidDetails.firsName) != "undefined")
     firstName = this.prepaidDetailForm.prepaidDetails.firsName;
Milan Chheda
  • 8,159
  • 3
  • 20
  • 35
  • that is a bit naughty , it should be kicked out automatically , not testing on a fixed key's value. ;) – SeleM May 24 '17 at 11:20
0

Please try following:

 for (var propName in this.prepaidObject) { 
    if (this.prepaidObject[propName] === undefined) {
      delete this.prepaidObject[propName];
    }
  }

And look at following answers: Remove blank attributes from an Object in Javascript

titech
  • 197
  • 4
  • 13
0
this.prepaidObject = {};
for (var i in this.prepaidDetailForm.prepaidDetails) {
  if (typeof(this.prepaidDetailForm.prepaidDetails[i]) !== 'undefined') {
    this.prepaidObject[i] = this.prepaidDetailForm.prepaidDetails[i];
  } 
} 

If you want to exclude also null values and undefined, change the condition to:

if (this.prepaidDetailForm.prepaidDetails[i] != null)

Not the double ==. because null == undefined but null !== undefined

quirimmo
  • 9,800
  • 3
  • 30
  • 45
0

Assume you have an object like below one,

var prepaidObject = {
    'customerType' : 'TestCustType',
    'firstName' : "sample FirstName",
    'lastName' : "sample LastName",
    'note' : undefined
 };

You can remove undefined using JSON.parse and JSON.stringify method,

JSON.parse(JSON.stringify(prepaidObject));
console.log(prepaidObject)

Output will be,

{
    'customerType' : 'TestCustType',
    'firstName' : "sample FirstName",
    'lastName' : "sample LastName"
 }
Manu Benjamin
  • 987
  • 9
  • 24