我有以下的方法来比较两个数组,一个来自props,另一个来自我的组件。我将props数组中存在但在我的组件数组中不存在的每个元素插入到第三个数组中,并添加一个名为"destroy : true"的属性,这样我就可以将其发送到后端从数据库中删除。
然而,由于某种原因,我的props被更新了,而不是我在方法中使用的变量。我不太确定为什么会这样,因为我没有直接引用prop,但我确实将其内容复制到方法中的变量中。
updateArray(){
let updatedArray = []
let oldArray = [...this.props.array]
oldArray.forEach(element => {
if(this.componentArray.indexOf(element) > -1){
updatedArray.push(element)
}else{
let newElement = element
newElement.destroy = true
updatedArray.push(newElement)
}
})
return updatedArray
},
为什么会发生这种情况?我的组件中的其他元素都正常工作,只有这个有问题。
Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号