-1

For example given

const a = {
  "a": 1,
  "b": "hi",
}

const c = "54"
const d = "66"

I want a to be

a = {
  "a": 1,
  "b": "hi",
  "c": 54,
  "d": 66,
}

I want to do it in a single line so

 a = {c, d}

But the above code will get rid of a, b. Any quick way to accomplish this?

VLAZ
  • 26,331
  • 9
  • 49
  • 67
Tree Garen
  • 205
  • 2
  • 9

2 Answers2

3

Spread syntax

const a = {
    a: 1,
    b: 'hi',
};

const c = '54';
const d = '66';

console.log({ ...a, c, d });
Nikita Madeev
  • 4,284
  • 9
  • 20
0

One way is to use Object.assign:

a = Object.assign({}, a, { c, d });

This effectively creates a new object ({}), then it copies all properties from your initial object (a) then it copies the new properties ({c, d }).

Ovidiu Dolha
  • 5,335
  • 1
  • 21
  • 30