0

I have the following object:

const original = [{
        "key-8": {
            "some object"
        }
    },
    {
        "key-12": {
            "some object"
        }
    },
    {
        "key-12": {
            "some object"
        }
    },
    {
        "key-1": {
            "some object"
        }
    },
    {
        "key-8": {
            "some object"
        }
    }
]

As you can see I have two objects with key-8 and two with key-12. I would like to combine them (the order does not matter) so the output will be:

{
    {
        "key-12": [{
                "some object"
            },
            {
                "some object"
            }
        ]
    }, {
        "key-1": {
            {
                "some object"
            }
        },
        {
            "key-8": [{
                    "some object"
                },
                {
                    "some object"
                }
            ]
        }
    }
}

I cannot make it works no matter what I try. I used reduce, I used regular forEach and other "hacks" simple do not work because TypeScript does not like them.

Can someone help please?

  • 2
    Does this answer your question? [How can I group an array of objects by key?](https://stackoverflow.com/questions/40774697/how-can-i-group-an-array-of-objects-by-key) – IISkullsII Sep 01 '22 at 13:47
  • @IISkullsII tried this, it does not work for me. probably i am doing something wrong :( – kolomichenko Sep 01 '22 at 13:49
  • Is there a reason why you've got `"..."` everywhere? It makes it hard to tell the difference between correct output and incorrect output (what if I just copy the first object over and over again)? Could you make this more of a [mre]? – jcalz Sep 01 '22 at 13:50
  • @jcalz agree, done – kolomichenko Sep 01 '22 at 13:58
  • Now you still have identical things, except they are no longer valid TS or JS (`{"some object"}` is a syntax error). I was saying you should make them have different contents so you could actually distinguish correct from incorrect output. – jcalz Sep 01 '22 at 13:59
  • Please fix the input/output so that it is valid JS. You can't have `{ {}, {} }` and you can't have `{""}` as objects. Also, do you *really* want the output to be an array-of-dictionaries-of-objects-or-arrays-of-objects? Like, if I fix your syntax errors and add distinguishing data then [this approach](//tsplay.dev/WkK30W) gives that output, but I'd think you'd rather have a single dictionary of arrays of objects like [this](//tsplay.dev/WvAQAm), which is simpler to implement and simpler to process. I'm happy to write either of those as an answer but only if the question is improved first. – jcalz Sep 01 '22 at 14:23

1 Answers1

0

try this

const orig = [
  {
    "key-8": {
      "key": "...",
      "value": "...",
      "info": "..."
    }
  },
  {
    "key-12": {
      "key": "...",
      "value": "...",
      "info": "..."
    }
  },
  {
    "key-12": {
      "key": "...",
      "value": "...",
      "info": "..."
    }
  },
  {
    "key-1": {
      "key": "...",
      "value": "...",
      "info": "..."
    }
  },
  {
    "key-8": {
      "key": "...",
      "value": "...",
      "info": "..."
    }
  }
];


const result = orig.reduce(function (r, a) {
    const { key, ...others } = Object.values(a)[0];
    r[Object.keys(a)] = r[Object.keys(a)] || [];
    r[Object.keys(a)].push(others);
    return r;
  }, Object.create(null));

  console.log(result);
jhonidie
  • 1
  • 2