I am working on a project where I have a class and a JSON object. My goal is to update the properties in the class based on the values in the JSON object, using Angular 9.
This is the class:
export class Searchdata{
name:boolean=false;
age:boolean=false;
summer:boolean=false;
winter:boolean=false;
football:boolean=false;
tennis:boolean=false;
}
And here is the JSON object:
[
{
"name": "name",
"visible": true,
},
{
"name": "age",
"visible": true
},
{
"name": "football",
"visible": true
}
]
The JSON object may not always have the same number of elements as the class has properties, but it can have all the same number of items for easier handling. I have explored various solutions, but for illustration purposes, below is how I envisioned the process working:
permPageData:any="The JsonObject";
tableSearchModel:Searchdata;
someFunction(){
this.tableSearchModel = new Searchdata();
permPageData.forEach(element => {
if(element.name == this.tableSearchModel["element.name"])
this.tableSearchModel[element.name] = element.visible;
}
return this.tableSearchModel;
}