我正在使用 delete
从数组中删除一个元素
我保存了这个对象:
obj = {
Variables: [ {
Name: "test",
Type: "Int",
Control: "U",
Value: "123"
}, {
Name: "ftr",
Type: "DateTime",
Control: "UA",
Value: "123123"
}, {
Name: "wertwe",
Type: "Int",
Control: "SA",
Value: "435345"
} ]
};
使用此代码:
delete data["Variables"][2];
现在 obj 包含值:
{"Variables":[{"Name":"test","Type":"Int","Control":"U","Value":"123"},{"Name":"ftr","Type":"DateTime","Control":"UA","Value":"123123"},null]}
有没有办法删除对象中不出现空值的元素?
最佳答案
你在阵列上刺穿了一个洞。您需要使用splice
来移除孔。我在写完这个答案后发现了这个骗局,所以我将投票结束示例所在的问答。
关于javascript - 从 JavaScript 数组中删除元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25792317/