The 3 JSON arrays have error details for type 1, 2 and 3 as shown below
const data1 =
[
  {
    "ErrorType": "Error-1A",
    "Error": "wrong ip address for 1A",
    "SERVER_COUNT": 7
  },
  {
    "ErrorType": "Error-1B",
    "Error": "password incorrect for 1B",
    "SERVER_COUNT": 6
  },
];
const data2 = 
[
  {
    "ErrorType": "Error-2A",
    "Error": "wrong data for 2A",
    "SERVER_COUNT": 8
  },
  {
    "ErrorType": "Error-2B",
    "Error": "password incorrect for 2B",
    "SERVER_COUNT": 3
  },
];
const data3 = 
[
  {
    "ErrorType": "Error-3A",
    "Error": "wrong data for 3A",
    "SERVER_COUNT": 1
  },
  {
    "ErrorType": "Error-3B",
    "Error": "password incorrect for 3C",
    "SERVER_COUNT": 5
  },
];
I want to combine the 3 JSON arrays data1, data2, data3 and the final JSON object should look as below:
{
  "details1": {
   "7": {
    "ErrorType": "Error-1A",
    "Error": "wrong ip address for 1A"
   },
      "6": {
    "ErrorType": "Error-1B",
    "Error": "password incorrect for 1B"
   }
},
  "details2": {
   "8": {
    "ErrorType": "Error-2A",
    "Error": "wrong ip address for 2A"
   },
      "3": {
    "ErrorType": "Error-2B",
    "Error": "password incorrect for 2B"
   }
},
  "details3": {
      "5": {
    "ErrorType": "Error-3B",
    "Error": "password incorrect for 3B"
   },
   "1": {
    "ErrorType": "Error-3A",
    "Error": "wrong ip address for 3A"
   }
}
}
Please note that Error-1A and Error-1B have the same count. Any two error types can have the same count.
I am using following function to loop over the array elements, turning each of them into an object property using the SERVER_COUNT property as the key.
let finalData = {
  details1: dataToDetails(data1),
  details2: dataToDetails(data2),
  details3: dataToDetails(data3)
};
function dataToDetails (data) {
  let result = {};
  data.forEach(({
                  ErrorType,
                  Error,
                  SERVER_COUNT
              }) => result[SERVER_COUNT] = result[SERVER_COUNT] ? [...result[SERVER_COUNT], {
    ErrorType,
    Error
}] : [{
    ErrorType,
    Error
}]);
  return result;
}
The above code is giving correct result. Only thing is that the SERVER_COUNT is not in the reverse order.
How to modify the above function so that for each of the types 1 2 3 I get SERVER_COUNT with highest values shown first?
Current output:
{
  "details1": {
   "6": {
    "ErrorType": "Error-1A",
    "Error": "wrong ip address for 1A"
   },
      "7": {
    "ErrorType": "Error-1B",
    "Error": "password incorrect for 1B"
   }
},
  "details2": {
      "3": {
    "ErrorType": "Error-2B",
    "Error": "password incorrect for 2B"
   },
   "8": {
    "ErrorType": "Error-2A",
    "Error": "wrong ip address for 2A"
   }
},
  "details3": {
   "1": {
    "ErrorType": "Error-3A",
    "Error": "wrong ip address for 3A"
   },
      "5": {
    "ErrorType": "Error-3B",
    "Error": "password incorrect for 3B"
   }
}
}
Desired output:
{
  "details1": {
   "7": {
    "ErrorType": "Error-1A",
    "Error": "wrong ip address for 1A"
   },
      "6": {
    "ErrorType": "Error-1B",
    "Error": "password incorrect for 1B"
   }
},
  "details2": {
   "8": {
    "ErrorType": "Error-2A",
    "Error": "wrong ip address for 2A"
   },
      "3": {
    "ErrorType": "Error-2B",
    "Error": "password incorrect for 2B"
   }
},
  "details3": {
      "5": {
    "ErrorType": "Error-3B",
    "Error": "password incorrect for 3B"
   },
   "1": {
    "ErrorType": "Error-3A",
    "Error": "wrong ip address for 3A"
   }
}
}
I am trying to use Map as following but it is giving null:
function dataToDetails(data) {
    let result = new Map();
    
    data.sort( ( { SERVER_COUNT: a }, { SERVER_COUNT: b } ) => b - a )
    data.forEach(
        ({ SERVER_COUNT, ...rest }) =>
            result.set(SERVER_COUNT, result.has(SERVER_COUNT)
                ? result.get(SERVER_COUNT).concat([rest])
                : [rest]),
    );
    return result;
}
