I am trying to split this array into two different arrays using the values inside the first array.
This is my initial array:
"carList": [
{
"brand": [
{
"model": [
{ "motor": { "id": 1 }, "color": 10 },
{ "motor": { "id": 2 }, "color": 20 },
{ "motor": { "id": 3 }, "color": 30 },
{ "motor": { "id": 4 }, "color": 40 }
]
}
]
},
{ "brand": [{ "model": [{ "size": 400 }] }] },
{ "brand": [{ "model": [{ "size": 500 }] }] }
],
The first array must contain the model with the motor and the colors. The second should only contain the brand with the size (I can have an indeterminate number of brands).
I would like this as an output:
"carList": [
{
"brand": [
{
"model": [
{ "motor": { "id": 1 }, "color": 10 },
{ "motor": { "id": 2 }, "color": 20 },
{ "motor": { "id": 3 }, "color": 30 },
{ "motor": { "id": 4 }, "color": 40 }
]
}
]
},
],
"carList": [
{ "brand": [{ "model": [{ "size": 400 }] }] },
{ "brand": [{ "model": [{ "size": 500 }] }] }
],
When trying to split the array, I tried to create the array only with size, but I get the carList global:
const carList1 = carList.filter((x) =>
x.brand.filter((y) => y.model.filter((z) => z.size)
);
How I can split my array and get this output?
UPDATE This is my more genral exemple test I can have many brand and model,
"carList": [
{
"brand": [
{
"model": [
{ "motor": { "id": 1 }, "color": 10 },
{ "motor": { "id": 2 }, "color": 20 },
{ "motor": { "id": 3 }, "color": 30 },
{ "motor": { "id": 4 }, "color": 40 }
]
},
{
"model": [
{ "motor": { "id": 1 }, "color": 11 },
{ "motor": { "id": 2 }, "color": 22 },
{ "motor": { "id": 3 }, "color": 33 },
{ "motor": { "id": 4 }, "color": 44 }
]
}
]
},
{ "brand": [
{
"model": [
{ "size": 400 },
]
},
{
"model": [
{ "size": 401 }
]
},
{ "brand": [{ "model": [{ "size": 500 }] }] }
],