0

I want to have this

  var myobject = {
        'a': [{ 'b': 2 }]
      };

      var myother = {
        'a': [{ 'c': 3 }]
      };

to become :

{'a' : [{'b': 2},{'c': 3}]}

how can I achieve this ?

Hypothesis
  • 1,208
  • 3
  • 17
  • 43
  • could you have more than one key of the `a` level? have you tried something? – Nina Scholz Jan 25 '19 at 20:17
  • So, you want to merge Objects, and concatenate Arrays in them. What about Objects with identical contents? Should they get duplicated? If you're willing to use lodash, this method might be useful: https://lodash.com/docs/4.17.11#defaultsDeep –  Jan 25 '19 at 20:19
  • What if `a` is not an array, but a plain object or primitive value? – trincot Jan 25 '19 at 20:22
  • @DoMiNeLa10 that does not produce the aforementioned object – Hypothesis Jan 25 '19 at 20:23
  • @Hypothesis yes, but it's a solution to a similar problem, and the one shown in the answer is pretty vague. –  Jan 25 '19 at 20:24
  • @trincot there is no what if, I need to generate exactly {'a' : [{'b': 2},{'c': 3}]} out of the cited objects – Hypothesis Jan 25 '19 at 20:24
  • So if it is *exactly* that, then do `result = {'a' : [{'b': 2},{'c': 3}]}`. If there is no variation in your input, then what is the issue? Where are you stuck? Anyway, this question is closed now. – trincot Jan 25 '19 at 20:28
  • @trincot The question is not closed, it has to be done dynamically. When map gets to a point where the object.key is the same, it has to merge the existing value and the new value together as myobject and myother – Hypothesis Jan 25 '19 at 20:33
  • The question has been closed -- look at the yellow banner at the top. All you need is in the referenced Q&A. – trincot Jan 25 '19 at 20:37

0 Answers0