i'm pretty new to ramdajs, however I do understand the concepts of functional programming so a little help with this challenge will set me off forward.
so I have two json responses related by a foreign Key relationship, carId
in this case. where a car can have many colors.
Cars:
[ [
{
name: { manufacture: 'toyatothon', year: '1830' },
density: 1.14,
id: '32ccfa8d-8db6-41f6-83fa-7726851bffd4',
image: 'https://test.image/toyotathon',
description: {
type: 'typical toyotathon',
style: 'stylish'
}
},
{
name: { manufacture: 'toyatothon', year: '1830' },
density: 1.14,
id: 'c7bc7177-8137-4552-a218-0aa4d2491373',
image: 'https://test.image/toyotathon',
description: {
type: 'typical toyotathon',
style: 'stylish'
}
},
... ]
*/
Colors:
[
{
id: '8f529b1f-18a8-408b-a88c-b9056ef6e50e',
name: { color: 'blue', shade: 'black'},
carId: 'c7bc7177-8137-4552-a218-0aa4d2491373',
image: 'https://test.image/pla_standard' },
{
id: 'b04458ec-ee2b-4ead-9f90-a2aa30608ea7',
name: { color: 'blue', shade: 'black'' },
carId: '32ccfa8d-8db6-41f6-83fa-7726851bffd4',
image: 'https://test.image/toyotathon'
},...]
desired output:
Cars:
[ [
{
name: { manufacture: 'toyatothon', year: '1830' },
density: 1.14,
id: '32ccfa8d-8db6-41f6-83fa-7726851bffd4',
image: 'https://test.image/toyotathon',
description: {
type: 'typical toyotathon',
style: 'stylish'
},
colors: [
{
id: 'b04458ec-ee2b-4ead-9f90-a2aa30608ea7',
name: { en: 'Sanded', de: 'Geschliffen', es: 'Estándar' },
carId: '32ccfa8d-8db6-41f6-83fa-7726851bffd4',
image: 'https://test.image/toyotathon'
},
... // whatever other colors belonging to the same `carId`
]}
{
name: { manufacture: 'toyatothon', year: '1830' },
density: 1.14,
id: 'c7bc7177-8137-4552-a218-0aa4d2491373',
image: 'https://test.image/toyotathon',
description: {
type: 'typical toyotathon',
style: 'stylish'
},
colors: [
{
id: 'b04458ec-ee2b-4ead-9f90-a2aa30608ea7',
name: { en: 'Sanded', de: 'Geschliffen', es: 'Estándar' },
carId: 'c7bc7177-8137-4552-a218-0aa4d2491373',
image: 'https://test.image/toyotathon'
},
... // whatever other colors belonging to the same `carId`
]
},
... ]
code that I have written achieves the above output but only kinda imperatively is as follows:
carsResponse.map((car) => {
car['colors'] = [];
colorsResponse.forEach((color) => {
if (car.id == color.carId) {
return car['colors'].push(color);
}
});
});
I'm wondering how the above map
with a forEach
inside can be achieved using ramdajs.