Home > Software engineering >  how to merge an array of objects by comparing one object in it
how to merge an array of objects by comparing one object in it

Time:09-03

The following is my array of objects

[
        {
            "date": "2022-08-26",
            "names": "TOTAL_REVENUE",
            "values": "253599593.31",
            "tabs": "Revenue"
        },
        {
            "date": "2022-08-26",
            "names": "TOTAL_VOICE_REVENUE",
            "values": "142706038.15",
            "tabs": "Revenue"
        },
        {
            "date": "2022-08-27",
            "names": "TOTAL_DATA_REVENUE",
            "values": "97825740.6",
            "tabs": "Revenue"
        },
        {
            "date": "2022-08-27",
            "names": "TOTAL_REVENUE_SM",
            "values": "6731513.29",
            "tabs": "Revenue"
        },
        {
            "date": "2022-08-27",
            "names": "TOTAL_BUNDLE_REVENUE",
            "values": "0",
            "tabs": "Revenue"
        }]

I want to club all the objects according to the date like the following

[
        {
            "date": "2022-08-26",
            "TOTAL_REVENUE": "253599593.31",
            "TOTAL_VOICE_REVENUE":"142706038.15"
        },
        {
            "date": "2022-08-27",
            "TOTAL_DATA_REVENUE": "97825740.6",
            "TOTAL_REVENUE_SM": "6731513.29",
            "TOTAL_BUNDLE_REVENUE": "0"
        }]

is there any way to format like this

CodePudding user response:

You can use a normal loop and an object to create the result. Like this:

  const input = [
    {
      date: "2022-08-26",
      names: "TOTAL_REVENUE",
      values: "253599593.31",
      tabs: "Revenue"
    },
    {
      date: "2022-08-26",
      names: "TOTAL_VOICE_REVENUE",
      values: "142706038.15",
      tabs: "Revenue"
    },
    {
      date: "2022-08-27",
      names: "TOTAL_DATA_REVENUE",
      values: "97825740.6",
      tabs: "Revenue"
    },
    {
      date: "2022-08-27",
      names: "TOTAL_REVENUE_SM",
      values: "6731513.29",
      tabs: "Revenue"
    },
    {
      date: "2022-08-27",
      names: "TOTAL_BUNDLE_REVENUE",
      values: "0",
      tabs: "Revenue"
    }
  ];

  function group(arr) {
    const dateGroups = {};
    for (let i = 0; i < arr.length;   i) {
      const { date, names, values } = arr[i];
      dateGroups[date] ??= {};
      dateGroups[date].date = date;
      dateGroups[date][names] ??= 0;
      dateGroups[date][names]  =  values;
    }
    return Object.values(dateGroups);
  }

  console.log(group(input));

CodePudding user response:

Assuming all the elements in input have a unique date/names combination, you could use the following:

function answer(input) {
  const revenue = new Map();
  
  for (const { date, names, values } of input) {
    if (!revenue.has(date)) revenue.set(date, { date });
    revenue.get(date)[names] = values;
  }
  
  return Array.from(revenue.values());
}

const input = [{
  date: "2022-08-26",
  names: "TOTAL_REVENUE",
  values: "253599593.31",
  tabs: "Revenue"
}, {
  date: "2022-08-26",
  names: "TOTAL_VOICE_REVENUE",
  values: "142706038.15",
  tabs: "Revenue"
}, {
  date: "2022-08-27",
  names: "TOTAL_DATA_REVENUE",
  values: "97825740.6",
  tabs: "Revenue"
}, {
  date: "2022-08-27",
  names: "TOTAL_REVENUE_SM",
  values: "6731513.29",
  tabs: "Revenue"
}, {
  date: "2022-08-27",
  names: "TOTAL_BUNDLE_REVENUE",
  values: "0",
  tabs: "Revenue"
}];

console.log(answer(input));

The code above stores the resulting values a Map instance, so we can easily check its presence and retrieve the value.

In the end the revenue structure looks like this:

revenue = Map{ // using object-notation to represent the Map contents
  "2022-08-26": {
    "date": "2022-08-26",
    "TOTAL_REVENUE": "253599593.31",
    "TOTAL_VOICE_REVENUE": "142706038.15"
  },
  "2022-08-27": {
    "date": "2022-08-27",
    "TOTAL_DATA_REVENUE": "97825740.6",
    "TOTAL_REVENUE_SM": "6731513.29",
    "TOTAL_BUNDLE_REVENUE": "0"
  }
}

Since you want an array containing the results and not a Map instance, I'll use Array.from(lookup.values()) to convert the structure into the desired result.

Note that revenue.get(date)[names] = values will replace the previous value with the same date/names combination. That's why this answer only works if all the date/names combinations are unique.

  • Related