У меня есть 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 не влияли изменения, сделанные в FilterRequirements?