0

I have an API that return me many object like this

{ id : 1243, string: "things", bool: 1, date: "2021-21-11"}
{ id : 4233, string: "somethingselse", bool: 1, date: "2021-21-11"}

I can have many datas for the same ID as

{ id : 1243, string: "other", bool: 0, date: "2021-27-10"}
{ id : 4233, string: "somethings else again", bool: 1, date: "2021-21-11"}

So i made a function to group by id my datas to get something like

object = {
        1243: [{
            0: {
                string: "things",
                bool: 1,
                date: "2021-21-11"
            },
            1: {
                string: "other",
                bool: 0,
                date: "2021-27-10"
            }
        }]

object2 = {
    4233: [{
        0: {
            string: "somethings else again",
            bool: 1,
            date: "2021-21-11"
        }
    }]



but now i want something like this

    alldata = [{
        1243: [{
            "2021-21-11": {
                string: "things",
                bool: 1
            },
            "2021-27-10": {
                string: "other",
                bool: 0
            }
        }],
        4233: [{
            "2021-21-11": {
                string: "somethings else again",
                bool: 1,
            }
        }]
    }]

i dont know how to do this, i tried to generate arrays, object but nothing great

Dakerl
  • 1
  • 1
    Where is the function you tried? – Ravi Kumar Gupta Nov 02 '21 at 09:45
  • Both your examples seem to have square or curly brackets that aren't needed. Not sure if that's just the example code or an actual issue with your conversion though. Anyway, you will want something like `if (!alldata[api_obj.id]) alldata[api_obj.id] = [];` to create the array if it doesn't exist, then `alldata[api_obj.id].push(api_obj);` to add the object to it. –  Nov 02 '21 at 09:48
  • (Also, always show your attempt. Ideally as [mre]. And please don't add irrelevant tags to your question; the first two are fine but neither angular or typescript is relevant here) –  Nov 02 '21 at 09:50

0 Answers0