mapConfig(config: IConfigItem[], dataModel: IDataModel): IConfigItem[] {
return config.map(c => {
const entries: [string, string][] = Object.entries(c);
entries.forEach(e => {
const configValue = e[0];
const configKey = e[1];
if (configKey in dataModel) {
c[configValue] = dataModel[configKey];
}
});
return { ...c };
});
}
У меня есть эта функция в классе обслуживания, и я вызываю этот метод из моего angular компонента.
const configCopy = [...this.config];
const dataModelCopy = { ...this.dataModel };
const mappedConfig: IConfigItem[] = this.loader.mapConfig(
configCopy,
dataModelCopy
);
Я создаю копию объекта this.config
и передаю это функция mapconfig
, чтобы он не обновлял базовый объект (this.config
), но всегда обновляет базовый объект this.config
. Не уверен, что я делаю что-то не так.