Grouping array nested value while comparing 2 objects - JavaScript


Suppose, we have the following JSON object −

const input = {
   "before": {
     "device": [
       {
         "id": "1234",
         "price": "10",
         "features": [
           {
             "name": "samsung",
             "price": "10"
           },
           {
             "name": "Apple",
             "price": "20"
           }
         ]
       },
       {
         "id": "2154",
         "price": "20",
         "features": [
           {
             "name": "samsung",
             "price": "30"
           },
           {
             "name": "Moto",
             "price": "40"
           }
         ]
       }
     ]
   },
   "after": {
     "device": [
       {
         "id": "1234",
         "price": "50",
         "features": [
           {
             "name": "samsung",
             "price": "20"
           },
           {
             "name": "Lenovo",
             "price": "30"
           }
         ]
       },
       {
         "id": "2158",
         "price": "40",
         "features": [
           {
             "name": "samsung",
             "price": "30"
           }
         ]
        }
     ]
 }
};

Our requirement is to group the id & its feature into a single row if the same id is there in the before & after object.

And the required output can be better described by the following visual representation.

We are required to write a function that groups and sorts the data in the same way.

Example

Following is the code −

const input = {
   "before": {
      "device": [
      {
         "id": "1234",
         "price": "10",
         "features": [
         {
            "name": "samsung",
            "price": "10"
         },
         {
            "name": "Apple",
            "price": "20"
         }
         ]
         },
         {
            "id": "2154",
            "price": "20",
            "features": [
            {
               "name": "samsung",
               "price": "30"
            },
            {
               "name": "Moto",
               "price": "40"
            }
            ]
         }
         ]
      },
      "after": {
         "device": [
         {
            "id": "1234",
            "price": "50",
            "features": [
            {
               "name": "samsung",
               "price": "20"
            },
            {
               "name": "Lenovo",
               "price": "30"
            }
            ]
         },
         {
            "id": "2158",
            "price": "40",
            "features": [
            {
               "name": "samsung",
               "price": "30"
            }
            ]
         }
      ]
   }  
};
const formatJSON = data => {
   const sub = Object.fromEntries(Object.keys(data).map(k => [k, 0]));
   return Object.values(Object.entries(data).reduce((r, [col, { device }])
   => {
      device.forEach(({ id, price, features }) => {
         r[id] = r[id] || [{ id, ...sub }];
         r[id][0][col] = price;
         features.forEach(({ name, price }) => {
            let temp = r[id].find(q => q.name === name);
            if (!temp){
               r[id].push(temp = { name, ...sub });
            };
            temp[col] = price;
         });
      });
      return r;
   }, {}));
};
console.log(formatJSON(input));

Output

This will produce the following output on console −

[
 [
   { id: '1234', before: '10', after: '50' },
   { name: 'samsung', before: '10', after: '20' },
   { name: 'Apple', before: '20', after: 0 },
   { name: 'Lenovo', before: 0, after: '30' }
 ],
 [
   { id: '2154', before: '20', after: 0 },
   { name: 'samsung', before: '30', after: 0 },
   { name: 'Moto', before: '40', after: 0 }
 ],
 [
   { id: '2158', before: 0, after: '40' },
   { name: 'samsung', before: 0, after: '30' }
 ]
]

Updated on: 01-Oct-2020

314 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements