Home > Software engineering >  javascript split array in two arrays
javascript split array in two arrays

Time:05-07

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

CodePudding user response:

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`.

  • Related