I put the same code also on jsbin: https://jsbin.com/literefeqo/edit?js,console
Explanation
I’ve got an array of objects (1) and would like to transform (possibly using map) this object. The transformation criteria is a given array (2) and corresponds to the german property in arrObj. That means, if there is a german property in arrObj it should be "copied" out and it should be used as a key to produce the resultObj (3). If there is no german property then a key should be "Unknown" or whatever.
Note: There can be more entries in resultObj for e.g. Montag. Thats way resultObj.Montag[i] should be an array of objects.
(1) Array of Objects
const arrObj = [
{
"day": {
"string": "Monday",
"Number": 1
},
"description": {
"type": "string",
"value": "The first day of the week"
},
"german": {
"type": "string",
"value": "Montag"
}
},
{
"day": {
"string": "Tuesday",
"Number": 2
},
"description": {
"type": "string",
"value": "The second day of the week"
}
},
{
"day": {
"string": "Wednesday",
"Number": 3
},
"description": {
"type": "string",
"value": "The third day of the week"
},
"german": {
"type": "string",
"value": "Mittwoch"
}
}
];
(2) Array that should become the key for the new object
const germanDays = ["Montag","Dienstag","Mittwoch","Donnerstag"];
(3) Result should look like
const resultObj = {
"Montag": [
{
"day": {
"string": "Monday",
"Number": 1
},
"description": {
"type": "string",
"value": "The first day of the week"
},
"german": {
"type": "string",
"value": "Montag"
}
}
],
"Dienstag": [
{}
],
"Mittwoch": [
{
"day": {
"string": "Wednesday",
"Number": 3
},
"description": {
"type": "string",
"value": "The third day of the week"
},
"german": {
"type": "string",
"value": "Mittwoch"
}
}
],
"Donnerstag": [
{}
],
"Unknown": [
{
"day": {
"string": "Tuesday",
"Number": 2
},
"description": {
"type": "string",
"value": "The second day of the week"
}
}
]
};