【发布时间】:2020-05-20 09:33:30
【问题描述】:
我想知道如何删除对象数组 javascript 中的特定键。
在数组对象obj中,如果id为null,则删除javascript中的键
var obj = [{
id: 1,
field: "finance"
}, {
id: null,
field: "service}, {
id: 2,
field: "information"
}]
functionremoveKey(arrobj) {
return arrobj.filter(e => {
if (e.id == null) {
delete e.id;
}
}
}
var result = removeKey(obj);
预期输出:
{
{ id: 1, field: "finance" },
{ field: "service" },
{ id: 2, field: "information" }
]
【问题讨论】:
-
这不是
.filter()的用途。你要么想要.forEach()要么.map() -
您在
service之后缺少报价。是不是笔误? -
试试这个
obj.forEach(e => { if (e.id === null) { delete e.id } });
标签: javascript jquery arrays object