0

JSON

{
 "cars":
{
  "12345": [1960, 1961, 1962],
  "4567": [2001, 2002]
}
}

HTML

<strong>Plate and year</strong>
<div *ngFor="let list of lists">
{{list.cars}}
</div>

I need to display like this:

Plate and year

12345- 1960, 1961, 1962.

4567- 2001, 2002.

0

1 Answer 1

2

Based on your data structure, you can achieve this using the KeyValuePipe and additional nested *ngFor. KeyValuePipe allows you to iterate over an object similar to Object.entries providing a key and value property for each item. In this case the value will be an array that you can iterate over using an *ngFor:

<strong>Plate and year</strong>
<div *ngFor="let list of lists">
  <div *ngFor="let car of list.cars | keyvalue">
    <div>{{car.key}} - <div *ngFor="let year of car.value">{{year}}</div>
    </div>
  </div>
</div>

Here is an example in action.

Hopefully that helps!

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

1 Comment

This is what i was looking for. Very clean and great way of using keyvalue pipe. Thanks a lot @AlexanderStaroselsky xoxo

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.