I have a NodeJS function which is returning the following JSON array. It has all errors of type 1:
const data1 =
[
{
"ErrorType": "Error-1A",
"Error": "wrong ip address for 1A",
"SERVER_COUNT": 9
},
{
"ErrorType": "Error-1B",
"Error": "password incorrect for 1B",
"SERVER_COUNT": 2
},
....
]
I have another JSON array for all errors of type 2:
const data2 =
[
{
"ErrorType": "Error-2A",
"Error": "wrong data for 2A",
"SERVER_COUNT": 8
},
{
"ErrorType": "Error-2B",
"Error": "password incorrect for 2B",
"SERVER_COUNT": 3
},
....
]
I have another JSON array for all errors of type 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": {
"9": {
"ErrorType": "Error-1A",
"Error": "wrong ip address for 1A"
},
"2": {
"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": {
"1": {
"ErrorType": "Error-3A",
"Error": "wrong ip address for 3A"
},
"5": {
"ErrorType": "Error-3B",
"Error": "password incorrect for 3B"
}
}
}
Please note that I want to add 3 new keys - details1, details2 and details3 - in the final response.
I also want to take out the SERVER_COUNT value and make it as a key.
I have added the following code but not sure to update it so as to get the final desired json
let finalData = Object.assign({}, ...data1.map(({
SERVER_COUNT,
...rest
}) => ({
[SERVER_COUNT]: rest
})));
Note: There are only 3 main keys and those are hard-coded details1 , details2, details13
[]around it when it should be{}