删除json数组中的数据

2024-01-05

我正在尝试从 json 数组中删除一段数据。例如我有这个数组

var favorites =    {
        "userID": "12345678",
        "favorites": [

            {   "name" : "My Favorites",
                "id" : "87654321",
                "items": 
                [
                    { 
                        "productID": "11234567",
                        "added": "TIMESTAMP",
                        "title": "Project",
                        "type": "Weekend Project",
                        "imageURL": "1"
                    },

                    { 
                        "productID": "11223456",
                        "added": "TIMESTAMP",
                        "title": "Bathroom",
                        "type": "Weekend Project",
                        "imageURL": "2"
                    },

                    { 
                        "productID": "11223345",
                        "added": "TIMESTAMP",
                        "title": "Curves",
                        "type": "Collections",
                        "imageURL": "3"
                    }
                ]
            },
            {   "name" : "Bathroom",
            "id" : "87654323",
            "items": 
            [
                { 
                    "productID": "11122224",
                    "added": "TIMESTAMP",
                    "title": "Project",
                    "type": "Weekend Project",
                    "imageURL": "1"
                },

                { 
                    "productID": "11122222",
                    "added": "TIMESTAMP",
                    "title": "Room",
                    "type": "Weekend Project",
                    "imageURL": "2"
                },

                { 
                    "productID": "11112222",
                    "added": "TIMESTAMP",
                    "title": "Strais",
                    "type": "Collections",
                    "imageURL": "3"
                },

                { 
                    "productID": "11111222",
                    "added": "TIMESTAMP",
                    "title": "Door",
                    "type": "Collections",
                    "imageURL": "4"
                }
            ]
        }
        ]
    } 

假设我想通过单击按钮将产品从浴室类别中删除。我将如何实现这一目标?

我一直在尝试这个但无济于事:

jQuery(document).on('click', ".removeFav", function() {
    favorites.favorites[1].items[1].splice();
}

我收到的错误:

未捕获的类型错误:对象#没有方法“拼接”


要取消设置任何变量,请使用delete陈述:

delete favorites.favorites[1].items[1]

这是正确的方法,并且它会起作用,但是如果您的目标是按顺序保留索引,那么您的方法splice方法是要走的路:

favorites.favorites[1].items.splice(1,1);

上面的代码将从第一个索引(第一个参数)开始删除一个元素(第二个参数)。

需要明确的是:要删除最后一个元素,请使用以下命令:

var arr = favorites.favorites[1].items;
arr.splice(arr.length - 1, 1);

在 JsFiddle 上查看您的代码 http://jsfiddle.net/yjcL5/.

如果数组未设置或为空,您可以采取其他措施来保护代码:

var arr = favorites.favorites[1].items;
if ( arr && arr.length ) {
    arr.splice(arr.length - 1, 1);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

删除json数组中的数据 的相关文章

随机推荐