How to update and assign an array of object to a new variable

I have an array of object which looks like ->

const test - [{id: '1', marks: '32'},{id: '2', marks: '31'},{id: '3', marks: '12'}]

Now, here I am trying to update this array of object so that I want to add one more key value in each object.

setPrevious = test => {
    const {
      nav: {
        queryParams: { bspId }
      }
    } = stores
    const copyTest = _.cloneDeep(test)
     this.productCommentsAdded =  copyTest?.forEach(marks => {
      marks.productId = bspId
    })
  }
  
  setPrevious(test)

with this, sets the this.productCommentsAdded to undefined . So, what is the proper way to do this ? update it and assign to that variable

51 thoughts on “How to update and assign an array of object to a new variable”

Leave a Comment