我有 MAinArrayMainArray=[{{First Name: "First Name"},{Last Name: "Contact"},{Last Name: "Contact"}]我想从这个数组中删除键值对,例如if (key == 'First Name'){delete MainArray[key] /// this doesnt work}最终结果必须是ResultArray=[{ {Last Name: "Contact"}, {Last Name: "Contact"} ]
2 回答
人到中年有点甜
TA贡献1895条经验 获得超7个赞
您可以使用当前对象的filter内部和内部,Object.keys并检查它是否包含给定的过滤器:
var MainArray=[ {'First Name': "First Name"}, {'Last Name': "Contact"}, {'Last Name': "Contact"} ];
var result = MainArray.filter(item=>!Object.keys(item).includes('First Name'));
var result2 = MainArray.filter(item=>!item['First Name']);
console.log(result);
console.log(result2);
扬帆大鱼
TA贡献1799条经验 获得超9个赞
filter()方法也可用于根据给定条件删除项目,请参见以下示例:
let data=[
{"First Name": "First Name"},
{"First Name": "First Name"},
{"Last Name": "Contact"},
{"Last Name": "Contact"}
]
const keyToRemove = "First Name"; //Key to remove
data = data.filter(x => !x[keyToRemove]);
console.log(data);
添加回答
举报
0/150
提交
取消