-1

How to cloning two object in one ? I tried Object.assign() but doesn't worked.

I have this : this.props.firstLetter[i] the return of that on my console is : firstLetter

This two objects I would like to create one big object of these two.

With Object.assign(), I tried this : Object.assign(this.props.firstLetter[i]), but the result of them is only one entry, the last only.

You have solution ?

Please.

EDIT 17/12/2017 :

Create array, and push those two object

KolaCaine
  • 2,037
  • 5
  • 19
  • 31
  • 1
    Possible duplicate of [How can I merge properties of two JavaScript objects dynamically?](https://stackoverflow.com/questions/171251/how-can-i-merge-properties-of-two-javascript-objects-dynamically) – NullDev Dec 17 '17 at 00:13
  • No, because if you see in my screenshot the key for this two object are the same and if I use Object.assign() he send me back only one object of in my case the second object. – KolaCaine Dec 17 '17 at 00:31

1 Answers1

2

Object.assign will effectively merge two objects, giving priority to the second one.

"cloning two objects into one" means overriding one objet with another, which yields you 1 object as result.

If your goal is to have two objects into one you have to assign different keys:

let group = {
  item1: obj1,
  item2: obj2 
}

If your goal (more likely) is to have a collection of same-type objects, group them into an array:

let groups = [
  obj1,
  obj2
]
weisk
  • 2,468
  • 1
  • 18
  • 18