gpt4 book ai didi

javascript - 从 json 数组中删除特定元素

转载 作者:行者123 更新时间:2023-11-30 13:54:54 25 4
gpt4 key购买 nike

我像这样声明一个 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)

请记住,这实际上改变了原始数组。

关于javascript - 从 json 数组中删除特定元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57499542/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com