-2

This is quite simple question, I got a Json like this:

const test = [
  {
    label: "Group 1",
    options: [
      { label: "option 1", value: "value_1" },
      { label: "option 2", value: "value_2" }
    ]
  },
  {
    label: "Group 1",
    opions: [
      { label: "option 3", value: "value_3" },
      { label: "option 4", value: "value_4" }
    ]
  },
  {
    label: "Group 2",
    options: [
      { label: "option 5", value: "value_5" },
      { label: "option 6", value: "value_6" }
    ]
  },
  {
    label: "Group 3",
    options: [
      { label: "option 7", value: "value_7" },
      { label: "option 8", value: "value_8" }
    ]
  },
  {
    label: "Group 3",
    options: [
      { label: "option 9", value: "value_9" },
      { label: "option 10", value: "value_10" }
    ]
  },
];

And I want to group them like this (by the Group label - the format is exactly as described below, I cannot add any other values as I'll use it in the react-select component as options)

const test = [
  {
    label: "Group 1",
    options: [
      { label: "option 1", value: "value_1" },
      { label: "option 2", value: "value_2" },
      { label: "option 3", value: "value_3" },
      { label: "option 4", value: "value_4" }
    ]
  },
  {
    label: "Group 2",
    options: [
      { label: "option 5", value: "value_5" },
      { label: "option 6", value: "value_6" }
    ]
  },
  {
    label: "Group 3",
    options: [
      { label: "option 7", value: "value_7" },
      { label: "option 8", value: "value_8" },
      { label: "option 9", value: "value_9" },
      { label: "option 10", value: "value_10" }
    ]
  },
];

How can I achieve that?

Thanks in advance

kivul
  • 1,148
  • 13
  • 28

1 Answers1

1

Please find the solution below for your problem statement.

const test = [{
    label: "Group 1",
    options: [{
        label: "option 1",
        value: "value_1"
      },
      {
        label: "option 2",
        value: "value_2"
      }
    ]
  },
  {
    label: "Group 1",
    opions: [{
        label: "option 3",
        value: "value_3"
      },
      {
        label: "option 4",
        value: "value_4"
      }
    ]
  },
  {
    label: "Group 2",
    options: [{
        label: "option 5",
        value: "value_5"
      },
      {
        label: "option 6",
        value: "value_6"
      }
    ]
  },
  {
    label: "Group 3",
    options: [{
        label: "option 7",
        value: "value_7"
      },
      {
        label: "option 8",
        value: "value_8"
      }
    ]
  },
  {
    label: "Group 3",
    options: [{
        label: "option 9",
        value: "value_9"
      },
      {
        label: "option 10",
        value: "value_10"
      }
    ]
  },
];

let temp = {};
test.forEach(t => {
  const key = t.label;
  if (temp[key]) {
    const tempArray = temp[key].options.concat(t.options);
    temp[key].options = tempArray;
  } else {
    temp[key] = {
      label: key,
      options: t.options
    }
  }
});

console.log(Object.values(temp));
Ayush
  • 131
  • 3