Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Javascript multi-dimentional array

i need some help to change multi-dimentional array of this data :

const dataArr: any[] = [
    {
      id: 1,
      orders: [{ id: 1, order_name: "john Doe" }],
      product: {
        id: 1,
        name: "Plate",
        price: "7",
      },
      location: {
        id: 30475986,
        name: "store_1",
      },
      serial_no: "223311",
      qty: 1,
    },
    {
      id: 2,
      orders: [{ id: 2, order_name: "jane Doe" }],
      product: {
        id: 1,
        name: "Plate",
        price: "7",
      },
      location: {
        id: 30475986,
        name: "store_1",
      },
      serial_no: "223313",
      qty: 1,
    },
  ];

in this case i must grouping the serial no & price inside the location, and it must be an array, then the locations groupping in orders also.
and this is output that i need to solved :

[
    {
      id: 1,
      name: "Plate",
      orders: [
        {
          id: 1,
          order_name: "john Doe",
          location: [
            {
              id: 30475987,
              name: "store_1",
              assets: [{ serial_no: "223311", price: "7" }],
            },
          ],
        },
        {
          id: 2,
          order_name: "jane Doe",
          location: [
            {
              id: 30475987,
              name: "store_1",
              assets: [{ serial_no: "223313", price: "7" }],
            },
          ],
        },
      ],
    },
  ]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

you can have something like this function to do transformation for you:

const transformData = (dataArr) => {
  const grouped = {};

  dataArr.forEach(item => {
    const productKey = item.product.id;
    const orderKey = item.orders[0].id;
    const locationKey = item.location.id;

    if (!grouped[productKey]) {
      grouped[productKey] = {
        id: item.product.id,
        name: item.product.name,
        orders: []
      };
    }

    let order = grouped[productKey].orders.find(o => o.id === orderKey);
    if (!order) {
      order = {
        id: orderKey,
        order_name: item.orders[0].order_name,
        location: []
      };
      grouped[productKey].orders.push(order);
    }

    let location = order.location.find(l => l.id === locationKey);
    if (!location) {
      location = {
        id: locationKey,
        name: item.location.name,
        assets: []
      };
      order.location.push(location);
    }

    location.assets.push({
      serial_no: item.serial_no,
      price: item.product.price
    });
  });

  return Object.values(grouped);
};

const transformed = transformData(dataArr);
console.log(transformed);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading