0

My simple code

const ob1 = {
  a: 'pokushevski',
  b: '2001',
};

const obj2 = {
  obj1: {},
  c: '1999',
};

const result = Object.assign(ob1, obj2);
console.log(result);
console.log(Object.getOwnPropertyNames(result));

Output

{ a: 'pokushevski', b: '2001', obj1: {}, c: '1999' }
[ 'a', 'b', 'obj1', 'c' ]

It seems that obj1 in result appears just as any other attribute,without having any reference to const obj1. Why?

4
  • 1
    Object.assign() mutates the object of the first parameter, you should use object destruction/spread: const result = { ...obj1, ...obj2 }; Commented Mar 5, 2021 at 7:25
  • Could you show us what your expected output is? Commented Mar 5, 2021 at 7:28
  • @HaoWu This won;t work,try it yourself. Commented Mar 5, 2021 at 7:29
  • 1
    const result = { ...obj2, ...{ obj1 } }; Maybe this is what you want Commented Mar 5, 2021 at 7:37

2 Answers 2

3

What you are doing right now is basically merging ob1 (without 'j') with obj2 (with 'j'), and since obj1 (with 'j') is an empty object inside of obj2, it gets added as a property on the resulting merged object.

What else did you expect, and can we help in getting you there?

Maybe you actually made a typo and wanted the first object ob1 to be named obj1 instead, then to be used as a property within obj2 and finally merge the two objects?

If so you co do:

const obj1 = {
  a: 'pokushevski',
  b: '2001',
};

const obj2 = {
  obj1, // shorthand syntax, property will be named the same as the const
  c: '1999',
};

// avoid mutation by using a new object as the target
const result = Object.assign({}, obj1, obj2);

// Display result and property names
console.log(result);
console.log(Object.getOwnPropertyNames(result));

If you only wanted to add obj1 within obj2 you don't need Object.assign

Watch out for what's mutable

With the code above, the original obj1 gets modified whenever you update result.obj1 properties. Try to update result.obj1.a and then console.log(obj1)

If you want to prevent this, you can use destructuring like so:

const obj1 = {
  a: 'pokushevski',
  b: '2001',
};

// Create a shallow copy of `obj1` to prevent mutating the original
// object by updating `obj2.obj1` properties
const obj2 = {
  obj1: {...obj1},
  c: '1999',
};

// Avoid mutating the original `obj1` by using a new object as the target
const result = Object.assign({}, obj1, obj2);

// Display result and property names
console.log(result);
console.log(Object.getOwnPropertyNames(result))

If you really want to keep using Object.assign instead of destructuring you can replace obj1: {...obj1} by obj1: Object.assign({}, obj1)

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

13 Comments

How to insert obj into obj2?
Updated answer based on my understanding
Yeah,it was typo.
From what the OP seemed to ask, he's expecting the following structure for result: { a: 'pokushevski', b: '2001', obj1: {a: 'pokushevski', b: '2001'}, c: '1999' }
I'm sorry for causing you trouble. I think I got it. Thank you so much your kind explanation :)
|
1

I guess that you want ob1 to be obj1 inside obj2

Then this is a way to do it:

const ob1 = {
  a: 'pokushevski',
  b: '2001',
};

const obj2 = {
  obj1: {},
  c: '1999',
};

const result = Object.assign({}, obj2);
Object.assign(result.obj1, ob1);
console.log(result);
console.log(Object.getOwnPropertyNames(result));

3 Comments

That's a very weird way to do it! Why assign?
because the question is titled "How to combine objects with objects.assign?"
I guess it all comes down to how mutable OP wants his original ob1 (or obj1) to be :)

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.