我有2个这样声明的属性:
ngOnInit() {
this.defaultRequirements = myData.property1.countryDocument; //should never change
this.filteredRequirements = myData.property1.countryDocument;
}
当我运行此onFilter函数时,defaultRequirements属性也会发生突变。
onFilter(selectedSections) {
let index = -1;
this.defaultRequirements.forEach(country => {
index++;
const filteredSectionsList = [];
country.section.forEach(section => {
selectedSections.value.forEach(selectedSelection => {
const selection = selectedSelection.split(/\s*[-]\s*/);
if (country.countryCode === selection[0]) {
console.log('matched');
filteredSectionsList.push(section);
}
});
const countryObj = Object.assign({}, country, {
section: [...filteredSectionsList]
})
// Here is the issue, when filtering, this.defaultRequirements also gets changed!
this.filteredRequirements[index] = countryObj;
})
})
}
问题
我不明白如何对this.filteredRequirements进行突变也对this.defaultRequirements进行突变(它们都等同于同一件事)!如何避免这种行为并使defaultRequirements不受filteredRequirements上所做更改的影响?
拉莫斯之舞
相关分类