0

I have a following consoled list of array how can i display the list?

enter image description here

I tried

this.item = this.data.["0"]  // this is throwing an error

is there any other workaround to achieve this?

5
  • Is the outermost array necessary? Do you have any control of the data structure you're receiving? Commented Mar 14, 2019 at 19:21
  • No i dont want the outermost array its completely unnecessary. And the data im receiving is pushed from other page. How can i slice off the the outer array? Because all the data will be coming from other page will be pushed inside the list as u can see in image Commented Mar 15, 2019 at 5:34
  • Have you tried this.item = this.data[0] ?? Commented Mar 15, 2019 at 6:15
  • Yes. It gives me an error this.item is undefined Commented Mar 15, 2019 at 6:41
  • Fetch you data by running inner loop. <div *ngFor="let a of data" > {{a.itemId}} <p *ngFor="let b of data.size">{{b.size}}</p> <p *ngFor="let c of data.price">{{c.price}}</p> </div> Commented Mar 15, 2019 at 7:45

2 Answers 2

3

You have an array in an array.

To display the list, you have to loop over the items in the array. Because your output is an array in an array, you can start the loop from the first element of the outer array.

Example:

<ion-list>
  <button ion-item *ngFor="let item of items[0]">
    {{ item.item_id }}
 </button>
</ion-list>
Sign up to request clarification or add additional context in comments.

7 Comments

Throws An Error "Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays."
Okay. Can you update your question with the function where you get the data? We might spot something there ...
@HarshMaisheri Happy to help! On to the next problem ;)
How can i access this in ts file? i tried this.items[0].item_id but it says cannot read property of item_id. can u help please
this.items[0] takes you to the first element in the outer array. From there you have three new arrays. To access the item._id of the second inner array, you could use this.items[0][1].item_id. But preferably you should rewrite your data input so you lose the outer (unneccessary) array.
|
0

Assuming your parent array is called data you can access the first element by using this.data[0]. You can also do other things such as loop over the items in the array, etc.

1 Comment

tired this.data[0] the output comes as undefined :(

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.