我试图从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:对象#没有方法'拼接'
答案 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);
如果数组未设置或为空,您可以采取其他措施来保护代码:
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添加到对象文字中,并在文字中包含您需要的方法。
包含此示例片段
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")