一: 深拷贝
深拷贝原数组出来,修改深拷贝的数组后,再把深拷贝的数组重新赋值到视图渲染数组。
this.directData.forEach((ele, index) => { if (ele.name === item) { let curEdit = deepCopy(this.editFormData) curEdit.goodsId = ele.id curEdit.goodsName = ele.name curEdit.goodsPhoto = ele.photo this.editFormData = deepCopy(curEdit) } })
二: 优雅的方法
let arr = this.directData.filter(ele => ele.id === item) let {id: goodsId, name: goodsName, photo: goodsPhoto} = arr.length && arr[0] this.editFormData = {...this.editFormData, ...{goodsId, goodsName, goodsPhoto}}