0

How can I change the data structure if there are identical types in the keys?
I get a data structure like this, is it normal practice to change the data on the frontend side?
I want to bring the general type to the top level and merge the rest of the data.

Data:

const data = [
    {
        id: 0,
        type: "type1",
        name: "",
        active: [],
        products: [{}],
        documents: [{}],
    },
    {
        id: 1,
        type: "type1",
        name: "",
        active: [],
        documents: [{}],
        products: [{}],
    },
    {
        id: 2,
        type: "type2",
        name: "",
        active: [],
        documents: [{}],
        products: [],
    }
]

Result:

const result = [
  {
    type: 'type1',
    merged: [
      {
        id: 0,
        name: '',
        active: [],
        products: [{}],
        documents: [{}],
      },
      {
        id: 1,
        name: '',
        active: [],
        products: [{}],
        documents: [{}],
      },
    ],
  },
  {
    type: 'type2',
    merged: [
      {
        id: 2,
        name: '',
        active: [],
        products: [{}],
        documents: [{}],
      },
    ],
  },
];
IncognitoUser
  • 312
  • 1
  • 8
  • This is called grouping. Just search for `groupBy in javascript` you'll find many answers – Amir Saleem Jul 05 '21 at 13:56
  • I found one here: https://stackoverflow.com/questions/14446511/most-efficient-method-to-groupby-on-an-array-of-objects – Amir Saleem Jul 05 '21 at 13:56
  • [My answer on this similar question](https://stackoverflow.com/a/65086435/12101554) looks close to what you want, you would just need to change a little bit – Samathingamajig Jul 05 '21 at 13:57
  • *"Is it normal practice..."* . Yes, absolutely. For example structure needed to pass to a chart library will differ from that needed to generate a table or a shopping component – charlietfl Jul 05 '21 at 13:59
  • Does this answer your question? [How to group an array of objects by key](https://stackoverflow.com/questions/40774697/how-to-group-an-array-of-objects-by-key) – pilchard Jul 05 '21 at 14:00

1 Answers1

1

const data = [
    {
        id: 0,
        type: "type1",
        name: "",
        active: [],
        products: [{}],
        documents: [{}],
    },
    {
        id: 1,
        type: "type1",
        name: "",
        active: [],
        documents: [{}],
        products: [{}],
    },
    {
        id: 2,
        type: "type2",
        name: "",
        active: [],
        documents: [{}],
        products: [],
    }
];

let unique_types = {};

data.forEach(item => {
  const toBeSaved = {
    id : item.id,
    name : item.name,
    active : item.active,
    products : item.products ,
    documents: item.documents
  }
  if(unique_types[item.type])
    {
      unique_types[item.type].merged.push(toBeSaved)
    }
  else 
    {
      unique_types[item.type] = {
        type : item.type,
        merged :  [toBeSaved]
      }
    }
})

console.log(Object.values(unique_types));
sazzad
  • 505
  • 4
  • 5