从json数组中删除数据

Delete data from json array

本文关键字:删除 数据 数组 json      更新时间:2024-04-28

我正试图从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上查看您的代码。

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

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

如果您想真正从数组中删除一个项,以便数组中位于该项之后的所有项都向下移动到较低的索引,您可以使用以下内容:

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

您希望对实际的items数组进行操作,这意味着要调用items数组上的方法。对于.splice(),您传递要开始修改数组的索引,然后传递要删除的项数,从而传递.splice(1, 1),它将删除从索引1开始的1个项。

我很可能会为此构建一个原型方法,使命令使用更加简单

// Place anywhere
Object.prototype.cut = function(start, elements){
    return this.items.splice(start, elements);
}
// Call using this
favorites.favorites[1].cut(1,1);

通过这种方式,您可以扩展方法并以非常灵活的方式处理数据。

===EDIT===

也许它就像蓝天所指出的那样灵活。更新了以下示例。我的风格是将收藏夹json添加到对象文字中,并在文字中包含您需要的方法。本例包括

  • JSON数据
  • 一种基于索引的图元裁剪方法
  • 一种基于索引的收藏夹获取方法
  • 一种基于参数名称值返回收藏夹的方法

代码段

var favorites = {
    data: {
        "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"
            }]
        }]
    },
    cut: function(favorite, start, elements) {
        return this.data.favorites[favorite].items.splice(start, elements);
    },
    get: function(favorite) {
        return this.data.favorites[favorite];
    },
    find: function(value, param) {
        var found;
        this.data.favorites.filter(function(item, i) {
            if (item[param] === value) {
                found = item;
                return;
            };
        })
        return found;
    }
};

要使用查找,只需执行类似的操作

favorites.find("Bathroom", "name")