[英]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.