如何从对象中删除未定义的属性?

时间:2017-05-24 11:05:21

标签: javascript typescript

我有这个对象:

  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
        };

现在有时某些属性是未定义的。我想要的是,如果this.prepaidDetailForm.prepaidDetails属性之一未定义,则不显示它们。因此,例如,如果this.prepaidDetailForm.prepaidDetails.firsName未定义,则无需创建'firstName'属性isnide对象。任何建议我怎么能这样做?

8 个答案:

答案 0 :(得分:4)

检查你的对象:

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

答案 1 :(得分:1)

一种方法是通过密钥并仅附加定义的密钥:

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

这假设prepaidObject中的密钥与prepaidDetails中的密钥相同,并且所有密钥都遵循您提及的相同规则。

特别是如果你正在使用ES6,你可以使用mapreduce这样做更优雅:

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;
     }, {});

使用更多ES6功能的更简洁的方法:

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 }), {});

答案 2 :(得分:1)

使用Lodash可以在一行中轻松完成:

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

这将删除所有 falsey

答案 3 :(得分:0)

您可能需要检查类型。

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

基本上:

var firstName = '';
if(typeof(this.prepaidDetailForm.prepaidDetails.firsName) != "undefined")
     firstName = this.prepaidDetailForm.prepaidDetails.firsName;

答案 4 :(得分:0)

请尝试以下:

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

看看以下答案: Remove blank attributes from an Object in Javascript

答案 5 :(得分: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 (this.prepaidDetailForm.prepaidDetails[i] != null)

不是双==。因为null == undefinednull !== undefined

答案 6 :(得分:0)

假设你有一个类似下面的对象,

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

您可以使用JSON.parse和JSON.stringify方法

删除undefined
JSON.parse(JSON.stringify(prepaidObject));
console.log(prepaidObject)

输出将是,

{
    'customerType' : 'TestCustType',
    'firstName' : "sample FirstName",
    'lastName' : "sample LastName"
 }

答案 7 :(得分:0)

最简单的方法是在parse之后stringify

确切语法:

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

示例

之前:

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

之后:

{
    'customerType' : 'TestCustType',
    'firstName' : "sample FirstName",
    'lastName' : "sample LastName"
 };