Angular 角度4拼接复制阵列对象
我为这个问题简化了一些代码Angular 角度4拼接复制阵列对象,angular,typescript,splice,Angular,Typescript,Splice,我为这个问题简化了一些代码 this.getDataRuleList.splice(this.count, 1, dataRuleData); console.log(this.getDataRuleList); this.count += 1; getDataRuleList正在从服务返回对象数组。它还可以毫无问题地绑定到一个预充涡轮工作台 // get method to get service collection get getDataRuleL
this.getDataRuleList.splice(this.count, 1, dataRuleData);
console.log(this.getDataRuleList);
this.count += 1;
getDataRuleList正在从服务返回对象数组。它还可以毫无问题地绑定到一个预充涡轮工作台
// get method to get service collection
get getDataRuleList(): IDataRule[] {
return this._dataRuleListService.dataRuleList;
}
当我编辑一行时,我试图通过用新对象替换整个对象来更新绑定数组(getDataRuleList)中的对象。我用的是拼接法。问题是,我替换的每个对象都变得相同(请参见图以获得更好的理解)。我粘贴在拼接值位置的值每次都不同(dataRuleData),但我的所有数组元素都变成了这个值。我假设这与参考文献有关,但如何避免这种情况发生
你可以
如果需要使用深度克隆,可以检查
你可以
如果需要使用深度克隆,可以检查
您可以为克隆使用es6拼接运算符 示例
const cloneData={…dataRuleData}代码>您可以为克隆使用es6拼接运算符
示例
const cloneData={…dataRuleData}代码>非常有效!非常简单和干净的解决方案。非常感谢。@Michael我建议您使用,它使用起来很简单:)效果很好!非常简单和干净的解决方案。非常感谢。@Michael我建议您使用,它使用起来很简单:)
const cloneData = Object.assign({},dataRuleData);
this.getDataRuleList.splice(this.count, 1,cloneData);
const deepCloneData = JSON.parse(JSON.stringify(obj1));