1

I have a JSON array of similar items , example of one item is as follow :

{
  "ownerID": "rkjgAs40NEuSJfp4jquNYQ",
  "defaultPriceScheduleID": null,
  "autoForward": false,
  "id": "44685751902",
  "name": "1864 End Leaf B18",
  "description": "1864 End Leaf B18",
  "quantityMultiplier": 1,
  "shipWeight": null,
  "shipHeight": null,
  "shipWidth": null,
  "shipLength": null,
  "active": true,
  "specCount": 26,
  "variantCount": 0,
  "shipFromAddressID": null,
  "inventory": null,
  "defaultSupplierID": null,
  "xp": {
    "SAP-Description": "1864 End Leaf B18",
    "Status": "Active",
    "Brand-Name": "!",
    "Product-Group": "Y6Z-vee5WUytwna-WsSxfw",
    "UPC-Each": "44685751902",
    "productAttributesComplete": true,
    "ImageURLs": [
      null,
      null,
      null,
      null,
      null,
      null
    ],
    "Ecommerce": {
      
    }
  }
}

I have a one dropdown in angular in which I want to bind the name to 'name' and value to 'product-group' but it says xp.product-group is undefined .

this is the code I am using to get the product list in ts file

getProductList(){
    this.service.getProductList().subscribe(data=>{
      this.ProductList=data;
      console.log(this.ProductList[0].xp['Product-Group']);
    });
  }

and here is dropdown html

<div class="form-group">
    <label for="pname">Product Name</label>
<select  (change)="onPOptionsSelected(mySelect.value)" #mySelect >  
    <option *ngFor="let prod of ProductList" [value]="prod.xp['Product-Group']"   class="form-control" id="pname"   >{{prod.name}} </option>  
</select>  
</div>
3
  • already tried its an error Commented May 31, 2021 at 4:35
  • can you share working example (stackblitz) of this code? This code is working fine Commented May 31, 2021 at 4:36
  • index 77 objects having the XP value as null, that root cause of that issue Commented May 31, 2021 at 5:12

2 Answers 2

1

Use [value]="prod.xp && prod.xp['Product-Group']" in dropdown html in case prod.xp is null or undefined.

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

Comments

0

can you please try prod.xp.product-Group to access the value of current item you want to access.

4 Comments

Please share your entire json object, I will share the working code from stackbliz.
[{"ownerID":"rkjgAs40NEuSJfp4jquNYQ","defaultPriceScheduleID":null,"autoForward":false,"id":"44685751902","name":"1864 End Leaf B18","description":"1864 End Leaf B18","quantityMultiplier":1,"shipWeight":null,"shipHeight":null,"shipWidth":null,"shipLength":null,"active":true,"specCount":26,"variantCount":0,"shipFromAddressID":null,"inventory":null,"defaultSupplierID":null,"xp":{"SAP-Description":"1864 End Leaf B18","Status":"Active","Brand-Name":"!","Product-Group":"Y6Z-vee5WUytwna-WsSxfw","UPC-Each":"44685751902","productAttributesComplete":true,"Ecommerce":{}}}]
Please have a look at this example stackblitz.com/edit/…

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.