从json数组中删除特定元素

时间:2019-08-14 17:36:55

标签: javascript jquery arrays json

我声明了这样的JSON

var json{
section1: [],
section2: [],
section3: []
} 

我想以这种方式或类似的方式删除特定项目

json[section][index].remove();

我尝试过这种方式

 delete json[section][index];

但是当我这样做时,数组元素不会重新排列

2 个答案:

答案 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);
相关问题