1

I need help getting the item and color value of each of the array inside the total array

This is an example of my json

 "results ": {
"totals": {
  "total": [
    {
      "item": "car",
      "color": "red"
    },
    {
      "item": "car",
      "color": "white"
    },
    {
      "item": "bike",
      "color": "green"
    }
  ]
},

This is the typescript code I'm using the get the item and color from my json response

  let i = 0;
         this .items = response.results.totals.total.forEach((x)=>
              {
                this .item = x.item ;
                this .color = x.color;
               i++
              }
          

HTML

 <pre><strong>Item</strong>{{item}}</pre>
  <pre><strong>Color</strong>{{color}}</pre>

When I run my code I'm only able to display the first array inside the total array

Items : car 
Color: red

How I can get all the values inside the total array and display in my page using html so my output look like this ?

Items : car
Color: red
Items : car
Color: white
Items : bike
Color: green 
2
  • You are not looping in your html as far as I can see, also not sure if just a typo in SO but you shouldn't have spaces after this for example (this. item) Commented Aug 10, 2020 at 22:18
  • Would recommend an *ngfor loop then you won't even need to create a loop in your typescript Commented Aug 10, 2020 at 22:20

1 Answer 1

1
this.items = response.results.totals.total;

Set the this.items as the array of the items you want to display.

And use ngFor for displaying the items in html:

<div *ngFor="let item of items">
  <pre><strong>Item</strong>{{item.item}}</pre>
  <pre><strong>Color</strong>{{item.color}}</pre>
</div>
Sign up to request clarification or add additional context in comments.

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.