繁体   English   中英

从json数组中删除特定元素

[英]Remove specific element from json array

我声明了这样的JSON

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

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

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

我尝试过这种方式

 delete json[section][index];

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

数组没有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) 

请记住,这实际上会更改原始数组。

slice()是您的朋友。

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice

json[section] = json[section].slice(index,index+1);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM