I am facing an issue in my Angular application where I need to convert a JSON object into an array. Although the mapping process is successful, the data within the array does not retain the properties and methods of my original object class. This hinders me from calling any custom functions created within the class.
export class Armor {
name: string;
listOfUpgradeMaterials: Material[];
get externalLink(): string {
return this.name.replace(" ", "_");
}
Here is how I am performing the conversion:
// Assuming 'result' is a JSON value with an array of armor objects
var list = <Armor[]>result;
console.log(list[0].externalLink); // This does not work as expected
console.log(list[0].name); // This works fine
I attempted using a custom map function, but it failed to instantiate the 'Material' property of the child objects. Therefore, that approach did not yield the desired outcome either.
this.list = _.map(this.list).map(function (x) {
let armor: Armor = Object.assign(new Armor, x);
return armor;
});
I am seeking advice on a comprehensive solution that can handle such scenarios, even when dealing with multiple levels of nested objects.
Thank you for your assistance.