2

I want to push a new key and corresponding value to an already existing object. My object is returned after subscribing to a WEB API endpoint. Within the object returned, I want to add a "QuantityOnHand" key and value. I have attempted the following, however, it is not as I want it.

branchProduct: BranchProduct;
getProductByName() {

     const productName = this.addProductForm.get('product')?.value;

     const quantity = this.addProductForm.get('quantity')?.value;

     this.branchService.getProductByName(productName).subscribe((resp: any) => {
      this.branchProduct = resp;
      this.branchProduct.QuantityOnHand = quantity;
      console.log(this.branchProduct);
     })

   }

The "branchProduct" interface class is as follows:

export interface BranchProduct {
  BranchId: number;
  ProductId: number;
  ProductTypeName: string;
  ProductName: string;
  QuantityOnHand: number;
  BaselineQuantity: number;
}

After assigning a value to the QuantityOnHand attribute, the object is logged as follows:

enter image description here

Would it be possible to set the QuantityOnHand as an actual additional attribute of the aforementioned object after the object is created? I have tried setting "branchProduct" as an array and pushing the value of the quantity through to it, however, I had no success doing so.

Any help is appreciated.

1
  • It seems like getProductByName returns an array, why is that? Can it return many products? If so, do you want to assign QuantityOnHand to all products? Commented Aug 27, 2021 at 0:44

3 Answers 3

1

In the picture it's showing that branchProduct data is in 0 index of array. If branchProduct data always be one record on 0 index then you can fix it like.

getProductByName() {
     const productName = this.addProductForm.get('product')?.value;
     const quantity = this.addProductForm.get('quantity')?.value;
     this.branchService.getProductByName(productName).subscribe((resp: any) => {
      this.branchProduct = resp;
      this.branchProduct[0].QuantityOnHand = quantity;
      console.log(this.branchProduct);
     })

   }

Other wise you need to a implement a loop to modify the data.

const branchProductArray = []
branchProductObj.map((product, index) => {
   let productObject = {... product };
    productObject.QuantityOnHand = quantity;
    branchProductArray.push(productObject)

})
console.log(branchProductArray). //will be desire result.
Sign up to request clarification or add additional context in comments.

1 Comment

I'd probably do this.branchProduct = resp[0] so the type doesn't need to be changed
1

this.branchProduct is an array.

  this.branchProduct[0].QuantityOnHand = quantity;

should work

Comments

0

Use spread operator

this.branchProduct = {...resp, quantityonHand:quantity}

1 Comment

Would you mind perhaps elaborating? I have added your answer to where I initially set this.branchProduct equal to resp, however, it displays the object similarly as to how I had it initially, However, now on top of that, it has the object array set as follows: {0: {...}, quantityOnHand: 4}

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.