This is my model class. I have to map the values from array to respective ngModel checking the title and pushing the result in ngModel value.
In component file
export class demoModel{
demo1:string
demo2:string
}
demo=new demoModel()
var data= [{title:"demo1",result:1},{title:"demo2",result:2}]
for (const iterator of this.data) {
console.log(iterator)
//works upto here
for (const key of Object.keys(this.demo)) {
this.demo[key]=iterator.result;
}
}
IN HTML
<input [(ngModel)]="demo1">
<input [(ngModel)]="demo2">
How to achieve this in angular?
I tried using Object.keys to map the values but it is not working.
Try mapping the data and the object's key , if data.title matches with the object's key , assign the data.result to the object's key.
export class demoModel {
demo1: string = '';
demo2: string = '';
}
demo = new demoModel();
var data = [{
title: "demo1",
result: 1
}, {
title: "demo2",
result: 2
}]
this.data.map(x => {
Object.keys(this.demo).map(y => {
if (y == x.title) {
this.demo[y] = x.result;
}
})
})`