从下面的 JSON 中,我得到了 areaQuotas 数组,其中一些数组的 name 属性不存在。
[
{
"forums": "",
"resource": {
"dhjName": "myvhp",
"dhj": {
"areaProgramValue": "123",
"areaQuotas": [
{
"areaQuotaValue": "1234",
"name": "acc"
},
{
"areaQuotaValue": "12345",
"name": "pro"
}
],
"methodType": "DGH",
}
},
"task": "create"
},
{
"forums": "",
"resource": {
"dhjName": "myvhp",
"dhj": {
"areaProgramValue": "123",
"areaQuotas": [
{
"areaQuotaValue": "1234",
"name": "acc"
},
{
"areaQuotaValue": "12345",
"name": "pro"
},
{
"areaQuotaValue": "5666"
},
{
"areaQuotaValue": "7666"
}
],
"methodType": "DGH",
}
},
"task": "create"
},
]
从下面的 JSON 中,我得到了 areaQuotas 数组,其中一些 name 属性不存在。
如何删除areaQuotas数组中所有未定义name属性的对象
我已经尝试过,如下所示
test = test.filter((obj) => typeof obj.resource.dhj.areaQuotas.name === 'undefined');
这是我的 fiddle
最佳答案
您可以将 forEach
与 filter
一起使用。
test.forEach(item => {
item.resource.dhj.areaQuotas = item.resource.dhj.areaQuotas.filter(
areaQuota => {
return areaQuota.hasOwnProperty('name');
}
);
});
这是更新后的 fiddle :https://jsfiddle.net/o2gxgz9r/65246/
关于javascript - 如何删除嵌套数组中存在名称属性未定义的 JSON 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51920880/