gpt4 book ai didi

javascript - 如何在javascript中删除空数组长度

转载 作者:行者123 更新时间:2023-12-03 20:54:17 25 4
gpt4 key购买 nike

我有一个对象。如果对象属性数组为空,我需要删除它。为了。例如,测试和书籍将被删除并给出其他值。

const PermissionObj = {
permission: [
{
"test": []
},
{
"books": []
},
{
"Journals": [
{
"label": "Can View",
"value": "can_view"
},
{
"label": "Can Create",
"value": "can_create"
}
]
},
{
"deal": [
{
"label": "Can update",
"value": "can_update"
},
{
"label": "Can delete",
"value": "can_delete"
}
]
}
]
};

我正在使用双循环来解决这个问题,但我真的很麻烦

最佳答案

使用filter()使用条件,使用Object.value()将Object转为Array

const result = PermissionObj.permission.filter(x => Object.values(x)[0].length !== 0);

  const PermissionObj = {
permission: [
{
test: []
},
{
books: []
},
{
Journals: [
{
label: "Can View",
value: "can_view"
},
{
label: "Can Create",
value: "can_create"
}
]
},
{
deal: [
{
label: "Can update",
value: "can_update"
},
{
label: "Can delete",
value: "can_delete"
}
]
}
]
};
const a = PermissionObj.permission.filter(x => Object.values(x)[0].length !== 0);
console.log(a);

关于javascript - 如何在javascript中删除空数组长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60849426/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com