0

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;
}
meallhour
  • 13,921
  • 21
  • 60
  • 117
  • You've over complicated this to such an extent that you've made it very difficult to achieve your goal. Since you have control of this, the data structure should be redesigned to accommodate your requirements. This is a simple thing to achieve with a more appropriate object structure. – Randy Casburn Oct 23 '20 at 15:10
  • 1
    You should take a look at this https://stackoverflow.com/questions/5525795/does-javascript-guarantee-object-property-order – Titus Oct 23 '20 at 15:13
  • I have updated my question and added how I am using `Map` to achieve the result. But it is giving null. can you take a look? – meallhour Oct 23 '20 at 15:38

0 Answers0