我有一个如下所示的对象
{
"containers": [
{
"fields": [
{},
{},
'associative1': {},
'associative2': {}
]
}
]
}
基本上,“fields”是一个数组,其中包含 fields[0]、fields[1] 和 fields[associative1]、fields[associative2] 等元素
我的问题是我可以动态删除“仅”编号的元素...... 所以最后“fields”应该只有 associative1、associative2 元素
最佳答案
创建一个看起来像您想要的那样的有效对象...
var a = [{abc:1},{xyz:2}];
a.associative1 = {};
a.associative2 = {};
console.log(a);
输出(来自 Chrome)
(2) [{…}, {…}, associative1: {…}, associative2: {…}]
0: {abc: 1}
1: {xyz: 2}
associative1: {}
associative2: {}
length: 2
您可以像这样删除编号的数组元素...
a.splice(0,a.length);
或者,如果 Array 对象包含在一个更大的对象中,这样的东西就会起作用......
c.containers[0].fields.splice(0,c.containers[0].fields.length)
关于JavaScript 动态删除编号数组元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46741624/