Skip to content
Advertisement

Group an object of array based on keys inside of array

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

Advertisement

Answer

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)
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement