1

I'm consuming an object from an api like this.

Object

I can get the data but cant get the ChildSimpleProducts Array exclusively.

here is the product.service function

  getProducts = (): Observable<IProducts[]> => this.http.get<IProducts[]>(`${this.apiUrl + this.productsUrl}`);

and here is the component function

  data: any = [];


productsArray: Observable<IProducts>[];
getProducts() {
this.productService.getProducts().subscribe(data => {
  this.data = data;
  this.productsArray = data["$values"];
  console.log(this.productsArray)
})

}

i have tried a few things but keep getting undefined and undefined objects

2
  • Did u try this.productsArray[0].childSimpleProducts? Commented Apr 21, 2020 at 18:22
  • Did u try data.$values; Commented Apr 21, 2020 at 18:24

3 Answers 3

3

Use forEach to loop through all the productsArray and get the childSimpleProducts,

getProducts() {
 this.productService.getProducts().subscribe(data => {
  this.data = data;
  this.productsArray = data["$values"];
  this.productsArray.forEach( e => console.log(e.childSimpleProducts);
  //this.productsArray.filter( e => e.childSimpleProducts); // This will return you an array of childSimpleProducts
 })
}
Sign up to request clarification or add additional context in comments.

Comments

1

If u get data from api correctly then data.$values return result

data: any = [];
productsArray: Observable<IProducts>[];
getProducts() {
    this.productService.getProducts().subscribe(data => {
      this.data = data;
      this.productsArray = data.$values;
      console.log(this.productsArray)
      this.productsArray.forEach(x=>{
          console.log(x.ChildSimpleProducts)
      })
    })
 }

3 Comments

@Çağrı, that part is already working, so i'm logging the the object but i want to get the child products exclusively and store them in a separate variable
Ahh sorry I understood wrongly. Then u can use foreach to enter each of nested result
@Çağrı u r copying my answer. :(
0

Use load dash library to cloneDeep array https://lodash.com/docs/#cloneDeep

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.