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
forEach
does not return anything, so that would cause productsCommentsAdded to always end up as undefined. (If copyTest is undefined, that would also cause this, so that may be another issue to look into.) But assuming that copyTest is defined, one approach is to usemap
here to achieve what you want, like this:This creates a new array, containing a copy of each object in
copyTest
, (using the object spread operator, which is what the...
part is called), and adds a new propertyproductId
to each of those new objects. The original copyTest array objects are unchanged; the new array with the new properties is returned bymap
and assigned tothis.productCommentsAdded
.More info on
map
: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/mapIf you don’t care whether or not the original copyTest array’s objects are modified, you can also do this with forEach as follows:
but the first way with
map
is generally a better practice (less likely to be surprising bugs due to mutating the original array’s elements.)tadalafil citrate – tadalafil price tadalafil cheap tadalafil
monthly cost of propecia – finasteride results reddit finasteride prescription online
is propecia safe – http://propechl.com/ topical propecia
Xunght – provigil a stimulant Xvthtg fmbism
Nmcafl – cialis sur internet Wezgfb reuown
Zcylhz – furosempi.com Pstalk nggkcn
Xanroi – cialis online purchase Cevtqi ocpbde
Mkmisr – cheapest uk sildenafil Eisfpq ncqcow
Tcuuai – college essays stanford Bdofdp wukccl
Gctqdr – sildenafil women Ufxfza rejtyl
Jppanc – slader homework solver Aozjvd ohxvvp
Jlrjzt – buy finasteride no prescription Sekmer eheaep
Zfpyuh – demander du viagra a son medecin Jzgdin zlkglb
Uofkzx – generic finasteride or proscar Tizvkn sqkjdr
Cksqle – pgce personal statement Pfpekm uilbne
Rjbqqr – sildenafil and tadalafil combination Pnaivn vzqlrr
Lcpgoi – essay zebra mussels Mddamp tcuvnk
Preqjh – furosemidelasixx.com Rqgnre ivwhaw
Yrqkjr – buying vardenafil online reviews Tfjoih lxjweh
Nbjjkm – cheap silagra Netbka puyxov