0

假设有一个名为'_arr'的数组,我从中删除了一个项目。删除之前,虽然我登录到控制台。问题是日志显示数组就好像该项目已被移除一样。我审查了Polymer Documentation中的数据系统,但仍然在挠头。从聚合物1.x中的阵列中删除项目时出现的奇怪行为

我错过了数据系统的工作方式,或者我应该在其他地方寻找原因?

编辑:_arr是一个字符串数组,我传递的事件,如:

this.fire('rmv-item' , {item: 'item content which is string'}); 

下面的代码

_removeItemFromArr: function(e) { 

    const index = this._arr.indexOf(e.detail.item) ; 
    console.log('array before remoivng item:' , this._arr , index); //item doesn't exist 

    if (index>-1) { this.splice('_arr' , index, 1 } 

    console.log('array after removing item: ' , this._arr , index); //item doesn't exist 
}, 
+0

你可以发布数组'_arr'的内容,你在'e'中传递了什么? – Ofisora

+1

也许我=索引? –

+0

我的坏,是的,我是索引,修复它,对不起 – TheeBen

回答

0

的问题是,事情正在做的正是你说的事情:控制台日志数组,最重要的是不是将数组记录为“在过去的某个特定时间”,它会在日志实际运行时记录数组。而且由于日志记录操作不同步,在实际将交叉引用和符号表链接的数据写入浏览器控制台时,您已经从数组中删除了数据,因此您所看到的是console.log看到的内容它实际上踢。

如果你想要一个真正的快照你的数组方式“当你调用日志”,不要记录数组,记录数组的副本,这是保证使用类似的东西同步生成slice()

const index = this._arr.indexOf(e.detail.item); 
console.log(`array before removing item at [${index}]: ${this._arr.slice()}`); 

而且工作很好。

+1

非常感谢您的回复!学到了新的东西:) – TheeBen