[英]How to remove undefined property from object?
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.我想要的是如果
this.prepaidDetailForm.prepaidDetails
属性之一未定义不显示它们。 So for example if this.prepaidDetailForm.prepaidDetails.firsName
is undefined i dont need to create 'firstName'
property isnide object.因此,例如,如果
this.prepaidDetailForm.prepaidDetails.firsName
未定义,我不需要创建'firstName'
属性 isside 对象。 Any suggestion how can i do that?任何建议我该怎么做?
Check your object:检查您的对象:
for( var m in this.prepaidObject ) {
if ( this.prepaidObject[m] == undefined ) {
delete this.prepaidObject[m];
}
}
The shortest way to do this is to parse
it after stringify
.执行此操作的最短方法是在
stringify
之后parse
它。
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"
};
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.这是假设在键
prepaidObject
是一样prepaidDetails
和所有的人都跟着你提到的同样的规则。
Especially if you're using ES6 you might make this more elegant using map
and reduce
like so:特别是如果您使用的是 ES6,您可以使用
map
和reduce
使其更加优雅,如下所示:
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:还有一种使用更多 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 }), {});
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;
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并查看以下答案: Remove blank attributes from an Object in Javascript
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
因为
null == undefined
但null !== undefined
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 方法删除 undefined,
JSON.parse(JSON.stringify(prepaidObject));
console.log(prepaidObject)
Output will be,输出将是,
{
'customerType' : 'TestCustType',
'firstName' : "sample FirstName",
'lastName' : "sample LastName"
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.