1

对不起,如果这是多余的,但我已经在这里搜索了几个问答,但我仍然无法弄清楚我做错了什么。我有一个保存为骨干集合的数组,我需要使用它的索引从该数组中删除一个对象:

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]
4

1 回答 1

2

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参数。

于 2013-08-06T21:15:42.000 回答