[英]Javascript/Backbone - Delete Array Object Using Index
Sorry if this is redundant, but I've searched through several Q&A's here but I still can't figure out what I'm doing wrong. 抱歉,这是多余的,但是我在这里搜索了多个问答,但仍然无法弄清楚自己在做什么。 I have an array saved as a backbone collection, and I need to delete an object from that array using its index:
我有一个保存为骨干集合的数组,我需要使用其索引从该数组中删除一个对象:
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);
},
Here is my Backbone Collection: 这是我的骨干收藏:
[Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object]
splice
actually modifies the collection, and returns the removed items. splice
实际上会修改集合,并返回删除的项目。 See the docs here: 在这里查看文档:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice
Try this instead: 尝试以下方法:
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);
},
Also note the howMany
parameter. 还要注意
howMany
参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.