I have an input array of objects, which each object has the following format:
{
titleID: string,
titleName: string,
1af23_red: number,
45ua6_blue: number
}
What I know is that:
In every object, there will be always the keys
titleID
andtitleName
and then I will have several keys that have the formatnumber_string
.The
titleID
andtitleName
values will be different among the different objectsThe rest of the keys (1af23_red, 45ua6_blue, etc) will be the same in all objects and they will all have the same format 'id_name' So, if the first object has 1af23_red and 45ua6_blue as keys, all the rest will also have just those keys.
The type of array that I want back has the following format:
{
color: {
id
name
},
data: Array<
{
title: {
id
name
},
rating
}
>
}
So, example of input:
[
{
titleId: 'a',
titleName: 'atitle',
'1af_red': 50
'ba2_blue': 40
},
{
titleId: 'b',
titleName: 'btitle',
'1af_red': 30
'ba2_blue': null
},
{
titleId: 'c',
titleName: 'ctitle',
'1af_red': null
'ba2_blue': 10
}
]
I would expect back:
[
{
color: {
id: '1af',
name: 'red'
},
data: [
{
title: {
id: 'a',
name: 'atitle',
},
rating: 50
},
{
title: {
id: 'b',
name: 'btitle',
},
rating: 30
},
{
title: {
id: 'c',
name: 'ctitle',
},
rating: null
}
]
},
{
color: {
id: 'ba2',
name: 'blue'
},
data: [
{
title: {
id: 'a',
name: 'atitle',
},
rating: 40
},
{
title: {
id: 'b',
name: 'btitle',
},
rating: null
},
{
title: {
id: 'c',
name: 'ctitle',
},
rating: 10
}
]
}
]
I have tried doing this conversion with map and reduce but I am stuck. Is there an easy way to accomplish this?