0

I have a class with an interface :

from another component in the code I read a json of my list of hero, and want to create a class for each hero (so it is easier to manipulate their data)

MY shop list CLASS =>

interface ShopItemInterface {
    name: string;
    image: string;
    price: number;
    description: string;
 }

 export class ShopItem implements ShopItemInterface {

    public name: string;
    public image: string;
    public price: number;
    public description: string;

     constructor(obj: object) {
        for (let key in obj) {
            // this doesn't work and I don't know why, it is always false
            if (this.hasOwnProperty(key)) {
                this[key] = obj[key];
            }

        }
    }
 }

LOADER COMPONENT CLASS =>

  ngOnInit() {
    this.http.get(this.jsonShopLocationFile).subscribe(res => {
      for (let i = 0; i < res['items'].length; i++) {
        this.shopItems.push(new ShopItem(res['items'][i]));
      }
      console.log(this.shopItems[0].name);
    });
  }

I can't find a way to correctly bind the json data to an object without listing all the parameters manually. ( which would be a mess and with 0 reusability)

How would you achieve that correctly ? Should I create a class and then directly call a function like hero.FromJSON(jsonObj) to manually set all the property? can I do this in some way in the constructor ?

thank you !

1 Answer 1

1

Because when you are constructing the object it does not have those properties, they are going to be undefined, just remove the test and it will work. Remember that interfaces are a TypeScript construct for the compiler and that you are running JavaScipt in your browser.

    for (let key in obj) {
        this[key] = obj[key];
    }
Sign up to request clarification or add additional context in comments.

5 Comments

Removing the test is not a good idea I believe, it will allow wrong object to create useless property. I will probably make a separated method to assign property from Json then
You need to understand that testing for that property is always going to return false. You cannot test for a property that has not been assigned yet.
I do understand now yes, but are there any better way? so that I am sure of the property that I set
Why are you implementing interfaces in the first place? Why not just use the interface? I cannot see any reason why you would need ShopItem and just rename ShopItemInterface to ShopItem. Then use this.shopItems = res['items'];
Because I tried and if I have a interface like the one above and then let's say i do my myItem: ShopItem = myResult as ShopItem; but that my result containt a property "uselessProperty : 1", myItem.uselessProperty will exist and I do not want it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.