Sometimes I need to push data to the array, but I have one different key (here I have key color and key name) and another part of the object is the same. I use IF/else to check this, but i wonder is there any better and cleanest way to handle this?
getData(passedData, dataControl: string) {
const data = [];
for (const value of passedData) {
if (dataControl === 'color') {
data.push({
color: value, ---> diff
username: this.userForm.value.username,
test: this.userForm.value.test
}
);
this.userForm.controls['data1'].setValue(data); ---> diff
} else {
data.push({
name: value, ---> diff
username: this.userForm.value.username,
test: this.userForm.value.test
}
);
this.userForm.controls['data2'].setValue(data); ---> diff
}
}
}
Thank you
dataControlat all. An object property is easily made from the value ofdataControl:{ [dataControl] : passedData.value}. See this SO thread \$\endgroup\$