0

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!!

3 Answers 3

2

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)

Sign up to request clarification or add additional context in comments.

Comments

1

Just assign to the properties you want:

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

Comments

0

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)

Comments

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.