我声明了这样的JSON
var json{
section1: [],
section2: [],
section3: []
}
我想以这种方式或类似的方式删除特定项目
json[section][index].remove();
我尝试过这种方式
delete json[section][index];
但是当我这样做时,数组元素不会重新排列
答案 0 :(得分:2)
数组没有remove
函数。请改用splice
:
var data = {section1: [1,2,3,4]};
const remove = (arr, key, index) => arr[key].splice(index,1)
remove(data,"section1",2)
console.log(data)
请记住,这实际上会更改原始数组。
答案 1 :(得分:0)
slice()
是你的朋友。
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice
json[section] = json[section].slice(index,index+1);