0

I'm receiving these results from an API:

{
  "97": {
    "Title": "This is a title",
  },
  "98": {
    "Title": "This is another title",
  }
}

I'm using this interface:

export interface Product {
  Title: string;
}

export interface SKU {
  [key: string]: Product
}

Does my interface looks alright? And how do I work with this type of stracture in Angular regarding *ngFor?

2
  • 1
    It looks okay to me. To use with ngFor you would have to convert this to an array or something you can iterate over Commented Oct 2, 2018 at 18:57
  • 1
    There's also the keyvalue pipe depending on what version you're using. Commented Oct 2, 2018 at 19:21

2 Answers 2

2

It looks okay I think , and for using this with ngFor I think you need to convert the object to array using map function .

    var arr = Object.keys(objectName).map((index)=>{
    let item = objectName[index];

    return item ;
});

and then using arr within *ngFor

Sign up to request clarification or add additional context in comments.

Comments

1

What you want to achieve is an array

[
    "97": {
        "Title": "This is a title",
     },
    "98": {
        "Title": "This is another title",
     }
]

to parse your API response as SKU[]. To achieve this structure you can either correct your server code (correct way to do it) or do some fancy object to array conversions (don‘t do).

After parsing this result in your component as SKU[] you can iterate in your view using *ngFor.

Comments

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.