从json数组中删除数据

时间:2013-12-08 20:49:41

标签: javascript json

我试图从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();
}

错误我收到了:

  

未捕获的TypeError:对象#没有方法'拼接'

3 个答案:

答案 0 :(得分:17)

要取消设置任何变量,请使用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);

See your code on JsFiddle

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

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

答案 1 :(得分:4)

如果你想从数组中实际删除一个项目,以便数组中的所有项目向下移动到较低的索引,你可以使用这样的东西:

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

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

答案 2 :(得分:2)

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

// Place anywhere
Object.prototype.cut = function(start, elements){
    return this.items.splice(start, elements);
}

// Call using this
favorites.favorites[1].cut(1,1);

通过这种方式,您可以非常灵活地扩展方法并使用数据。

==编辑==

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

包含此示例
  • JSON数据
  • 基于索引
  • 剪切元素的方法
  • 基于索引获取收藏的方法
  • 基于参数name-value
  • 返回收藏夹的方法

片段

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;
    }
};

使用find只需做这样的事情

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