0
const fetchData = Object { data: [], status: 200, statusText: "OK", headers: {…}, config: {…}, request: {} }

How do i add below object into above fetchdata.data object

const priceModel = {id:1, name:"abc"}

result should be like below

const fetchData = Object { data: {[], priceModel:{id:1, name:"abc"}}, status: 200, statusText: "OK", headers: {…}, config: {…}, request: {} }
shabeeb ck
  • 101
  • 10
  • `mergedObject = {...fetchData, priceModel}` – Yevhen Horbunkov May 13 '20 at 20:29
  • this way i can add into fetchData.data ? – shabeeb ck May 13 '20 at 20:38
  • @shabeeb_ck : no, above will return you the *new object* which will have all the properties of `fetchData` and `priceModel` property that will hold corresponding object. Isn't that what you need? – Yevhen Horbunkov May 13 '20 at 20:39
  • ```mergedObject = {...fetchData, priceModel}``` . Here triple dot is a spread syntax. For More info [refer this stackoverflow question](https://stackoverflow.com/questions/31048953/what-do-these-three-dots-in-react-do) or [this blog](https://codeburst.io/javascript-es6-the-spread-syntax-f5c35525f754) – Ram May 15 '20 at 16:04

1 Answers1

0

Like the comment above mentions you can use the spread operator if you want to add it as a key in the main object.

If you want to assign it into data then you can simply

fetchData.data= {data:fetchData.data, priceModel}

This will keep the data array as you had it. if you want to merge priceModel into the data, given that you have shown data as an array, you can

fetchData.data.push({priceModel})

Hope this helps!

Alboman
  • 325
  • 1
  • 6