Javascript/Backbone-使用索引删除数组对象
Javascript/Backbone - Delete Array Object Using Index
如果这是多余的,很抱歉,但我已经搜索了几个问答;A在这里,但我仍然不明白我做错了什么。我有一个数组保存为主干集合,我需要使用它的索引从该数组中删除一个对象:
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
实际上修改了集合,并返回删除的项。请参阅此处的文档:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/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
参数。
相关文章:
- Mongoose-在更新中删除数组元素
- 在特定索引处剥离/删除数组中的值
- Framework7:使用swipeout删除数组中的项
- Javascript删除数组中某个位置之后的项
- 通过选中和取消选中复选框来添加和删除数组中的值
- 编写删除数组并添加到列表的函数
- Javascript - 删除数组中的重复值
- 在 Kineticjs 中删除数组中的组
- 如何根据数组内容添加/删除数组中的元素
- 用于删除数组中的零的Javascript函数代码
- Javascript-仅使用pop()方法删除数组中的负值
- 删除数组元素并将它们添加回原来的位置
- 如果一个键匹配,如何删除数组元素
- 无法使用拼接删除数组中的元素
- 如何从ko.computed中删除数组项
- 删除数组中的重复项,为什么此代码不起作用
- 使用另一个数组按索引删除数组中的对象
- 在 jquery 中,如何通过索引[“键”] 或值删除数组元素
- 使用 Lodash 删除数组中的元素
- 按索引删除数组元素