grooot
grooot

Reputation: 466

add multiple objects to object

I want to combine and add few objects into a new object.


let data = {}

let status = true

let res = [{
    "quantity": 82,
    "startDate": "2021-05-04T02:09:00Z",
    "unit": "bpm"
  },
  {
    "quantity": 79,
    "startDate": "2021-05-09T02:09:00Z",
    "unit": "bpm"
  }
]

Expected output

data = {

    "status" : true,

    "res" : [{
    "quantity": 82,
    "startDate": "2021-05-04T02:09:00Z",
    "unit": "bpm"
  },
  {
    "quantity": 79,
    "startDate": "2021-05-09T02:09:00Z",
    "unit": "bpm"
  }
]


}

I need to add "status" and "res" into "data" object.
How can i do this??
Any suggestion would be great!!

Upvotes: 0

Views: 4005

Answers (3)

Efrain Garay
Efrain Garay

Reputation: 36

I think that's what you need

let data = {}

let status = true

let res = [{
    "quantity": 82,
    "startDate": "2021-05-04T02:09:00Z",
    "unit": "bpm"
  },
  {
    "quantity": 79,
    "startDate": "2021-05-09T02:09:00Z",
    "unit": "bpm"
  }
]
 
data = {status,...res}

console.debug(data)

Upvotes: 0

Reinis
Reinis

Reputation: 505

The following command may be readable, understandable.

let data = {}

let status = true

let res = [{
    "quantity": 82,
    "startDate": "2021-05-04T02:09:00Z",
    "unit": "bpm"
  },
  {
    "quantity": 79,
    "startDate": "2021-05-09T02:09:00Z",
    "unit": "bpm"
  }
]

data = {res, status} // short command of data = { res: res, status: status}
console.log(data)

Upvotes: 2

Barmar
Barmar

Reputation: 780724

Just assign to the properties you want:

data.status = status;
data.res = res;

Upvotes: 1

Related Questions