0

I am receiving a JSON array called products. I am looping through it using ngFor.

*ngFor="let product of products"

each product also has an array in it, I am looping through that as follows

*ngFor="let item of product.Items"

I need to set a variable equal to product.items in ngOnInit. something like this:

this.items = product.Items

But I can't figure out how to properly access the child of the parent array (products) and assign the array in the child to a variable in ngOnInit.

6
  • You mean ngOnInit, correct? Commented Apr 6, 2017 at 6:24
  • Please post your ngOnInit(). Since products is an array, I don't understand which product's items are you trying to capture? Is there always only one product in this array? Commented Apr 6, 2017 at 6:28
  • You have an array of products. So to access one of them, you would use products[i], where i is the index of the product you want to access in the array. The items of that product is products[i].Items. It's not clear why you would need to have the Items of one product in a component having several products. Commented Apr 6, 2017 at 6:29
  • Could you go into the use case a little bit more? It's not clear if you need to single out a particular product's items, or if you want to get an array or object of just the items from each product. Commented Apr 6, 2017 at 6:33
  • 1
    Every product(child) of products have an array, I want to be able to go through all product's not just single out one. At the moment, I am going through them using (item of product.items) although this works for looping through and getting the data. But I need to get to items array in a filter function. Thats why I want to set product.items = to a variable called items. So I can loop through it using item of items this way I will have access to a variable called items in my filter function. Commented Apr 6, 2017 at 6:43

2 Answers 2

1

If you are working in ngOnInit(), you are dealing with the page lifecycle, so your code is independent of the *ngFor interface loops that you already created.

If you want to do something with each item in each object, such as wrapping them in markup, you could do something like this:

products.map(product => {
  product.items.map(item => "<p>" + item + "</p>")
  return product;
});

This would give you your original products array, with each item in each array wrapped in some markup.

Basically, Array.map() is a built in function for arrays that allows you to run a callback on each item in the array.

By nesting one map inside of another, you can cleanly run code on all items in all products.

Here is the MDN documentation for Array.map().

Using the same logic, you could do more or less whatever you want to each item in each array using the Array.map function.

In non-ES6 formatting, it would look more like this:

products.map(function(product){
  product.items.map(function(item){
    /* You can do what you want with each item here */
    return "<p>" + item + "</p>";
  });
  return product;
});

In the above example, I am simply wrapping an item in a <p> tag, but you could do more sophisticated processing to your items, with conditional logic, conversions, etc.

The only thing you need to do is to return something that will be the "new" version of that item.

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

6 Comments

My pleasure! Glad to help. Good luck with your work.
Just a quick question, if you have a minute products.map(product => { product.items.map(item => "<p>" + item + "</p>") this.items = product.items; }); this.items print out exactly what I want in the console, but for some reason variable items doesn't work in the view.
You probably have to set your products variable to the function call.
so it would be something like this.products = products.map(product => { product.items.map(item => "<p>" + item + "</p>") this.items = product.items; }); The key being the this.products= part.
Does that make sense?
|
0

Sounds like using a simple for loop in your ngOnInit function might solve your problem. Try something like this:

First create a variable to hold your array.

this.items = [];

Then iterate over your child array and push each item onto your new array:

ngOnInit() {
  for (let i = 0; i < product.Items.length; i++) {
    let currentItem = product.Items[i];
    this.items.push(currentItem);
  }
}

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.