Home > other >  Group an object of array based on keys inside of array
Group an object of array based on keys inside of array

Time:09-12

Guys this is the most complicated things that i've ever met. So, i have an array looks like this. I have an array that consist of Object that also consist of array who had items of name, and value.

I wanted to extract the value from the array and merged it with the keys, and then group it into an object.

[
    {
    "Penunjang Kerja": [
        {
            "name": "Penunjang Kerja",
            "value": "0"
        }
    ],
    "Kesehatan": [
        {
            "name": "Kesehatan",
            "value": "0"
        }
    ],
    "Ngawur": [
        {
            "name": "Ngawur",
            "value": "0"
        }
    ],
    "Sukidayo": [
        {
            "name": "Sukidayo",
            "value": "0"
        }
    ]
},
{
    "Penunjang Kerja": [
        {
            "name": "Penunjang Kerja",
            "value": "0"
        }
    ],
    "Kesehatan": [
        {
            "name": "Kesehatan",
            "value": "0"
        }
    ],
    "Ngawur": [
        {
            "name": "Ngawur",
            "value": "0"
        }
    ],
    "Sukidayo": [
        {
            "name": "Sukidayo",
            "value": "0"
        }
    ]
},
{
    "Penunjang Kerja": [
        {
            "name": "Penunjang Kerja",
            "value": "0"
        }
    ],
    "Kesehatan": [
        {
            "name": "Kesehatan",
            "value": "0"
        }
    ],
    "Ngawur": [
        {
            "name": "Ngawur",
            "value": "0"
        }
    ],
    "Sukidayo": [
        {
            "name": "Sukidayo",
            "value": "0"
        }
    ]
     },
   ]

I want my array to become looks like this

{ "Penunjang Kerja": [0, 0, 1, 0], //value
   "Kesehatan": [0, 0, 0, 0],
    ...rest
 }

Does anyone can help me ? Iam using lodash so, i think that might help

CodePudding user response:

The result here doesn't match what you've typed out as the expected result, but the logic is sound and your expected result has some extra data in it. You can use Array.reduce as an iterator, and then an Array.forEach loop on the object keys to extract the value.

let result = data.reduce((b, a) => {
  Object.keys(a).forEach(o => {
    b[o] = b[o] || [];
    b[o].push(a[o][0].value)
  });
  return b
}, {})

let data = [{
    "Penunjang Kerja": [{
      "name": "Penunjang Kerja",
      "value": "0"
    }],
    "Kesehatan": [{
      "name": "Kesehatan",
      "value": "0"
    }],
    "Ngawur": [{
      "name": "Ngawur",
      "value": "0"
    }],
    "Sukidayo": [{
      "name": "Sukidayo",
      "value": "0"
    }]
  },
  {
    "Penunjang Kerja": [{
      "name": "Penunjang Kerja",
      "value": "0"
    }],
    "Kesehatan": [{
      "name": "Kesehatan",
      "value": "0"
    }],
    "Ngawur": [{
      "name": "Ngawur",
      "value": "0"
    }],
    "Sukidayo": [{
      "name": "Sukidayo",
      "value": "0"
    }]
  },
  {
    "Penunjang Kerja": [{
      "name": "Penunjang Kerja",
      "value": "0"
    }],
    "Kesehatan": [{
      "name": "Kesehatan",
      "value": "0"
    }],
    "Ngawur": [{
      "name": "Ngawur",
      "value": "0"
    }],
    "Sukidayo": [{
      "name": "Sukidayo",
      "value": "0"
    }]
  },
]


let result = data.reduce((b, a) => {
  Object.keys(a).forEach(o => {
    b[o] = b[o] || [];
    b[o].push(a[o][0].value)
  });
  return b
}, {})

console.log(result)

  • Related