0

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 }] }] }
    ],

1 Answer 1

1

This would be one way of doing it:

const data={"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 }]}]} 
]}

const [mo,si]=["motor","size"].map(p=>data.carList.filter(e=>
  e.brand.find(b=>b.model.find(m=>m[p]))));

console.log(JSON.stringify(mo));
console.log(JSON.stringify(si));

I applied the snippet now to the updated data. The result arrays motor and sizes are calculated by applying a .filter() on the data array.

In this updated version of the script I assign a brand to the mo or si array if it somehow contains the property motor or size`.

Sign up to request clarification or add additional context in comments.

5 Comments

Yes its works but if i have two models in the brand like that :
Could you maybe extend your sample data to show this more general test case and explain exactly what you would like to happen, if in one brand branch we should find both, a motor and a size type model.
I update my exemple with the more general test case
I can have a number inderterminate of model, brand, i must loop on the brand
thank you @Carsten Massmann, it's perfect i can adapt for my project

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.