I want to convert object literal list from JSON file to particular class object list in javascript, I tried but not able to achieve, can anybody knows how to achieve this in ES5/ES6, since Im trying this in angular 2:
Here is my JSON file:
{"list":[
{"name":"ABC", "cost":200},
{"name":"LMN", "cost":100},
{"name":"POP", "cost":200},
{"name":"OEE", "cost":560},
{"name":"WWO", "cost":450},
{"name":"ERR", "cost":150},
{"name":"OWE", "cost":250}
]}
Product Class :
export class Product{
static newId:number = 0;
constructor(public name: string = "", public cost: number = 0,public id: number = 0){
this.id = ++Product.newId;
}};
Here "list" array contains list of object literals of type Object, I just want to convert all of them into the object of type "Porduct"
Here is what im tring to do:
this.appService.getProductList().subscribe(
data => this.productList = data.list,
error => console.error("error"),
() => console.log("GET done.")
);
Here "appService" is http service, "getProductList()" is service method returns observable, and "this.productList" is an array, I want to fill this array with object of type Product rather simple "Object". Please help me in this.