Javascript/主干-使用索引删除数组对象
对不起,如果这是多余的,但我已经在这里搜索了几个问答,但我仍然无法找出我做错了什么。我将一个数组保存为主干集合,需要使用其索引从该数组中删除一个对象:Javascript/主干-使用索引删除数组对象,javascript,jquery,backbone.js,Javascript,Jquery,Backbone.js,对不起,如果这是多余的,但我已经在这里搜索了几个问答,但我仍然无法找出我做错了什么。我将一个数组保存为主干集合,需要使用其索引从该数组中删除一个对象: deleteCartItem: function(e) { var itemIndex = $(e.currentTarget).attr( "data-index" ); console.log(itemIndex) console.log(this.collection) console.log(this.c
deleteCartItem: function(e) {
var itemIndex = $(e.currentTarget).attr( "data-index" );
console.log(itemIndex)
console.log(this.collection)
console.log(this.collection.length)
var newCollection = this.collection.splice(itemIndex);
console.log(newCollection.length);
},
以下是我的主要收藏:
[Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object]
splice
实际上修改集合,并返回删除的项。请参见此处的文档:
请尝试以下方法:
deleteCartItem: function(e) {
var itemIndex = $(e.currentTarget).attr( "data-index" );
console.log(itemIndex)
console.log(this.collection)
console.log(this.collection.length)
this.collection.splice(itemIndex, 1);
console.log(this.collection.length);
},
还要注意howMany
参数