Home > OS >  Make n number of arrays using the value from the same object
Make n number of arrays using the value from the same object

Time:11-09

I am currently trying to separate the orderItems and display them as individual. Right now if the user wants to add new item, and the item already exists, it just increments the quantity and does not duplicate them.

As a result I got this

[
  {
    "id": "cla7dnfln000mvprgde8b9ovh",
    "name": null,
    "price": 134,
    "quantity": 3,
    "orderId": "cla7dmh5v000ivprghh27g22z",
    "menuItemId": "cl9t45hry002uvpr9sp9jxl1t",
    "comments": null,
    "stock": null,
    "menuItem": {
      "id": "cl9t45hry002uvpr9sp9jxl1t",
      "image": "https://madre-cafe.com/wp-content/uploads/2021/11/logo-madre-cafe-header.svg",
      "name": "Especiales Item #2",
      "description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
      "price": "134",
      "available": true,
      "menuCategoryId": "cl9t45hqu000tvpr9fjw67ype"
    }
  },
  {
    "id": "cla8ehie20000vpusxqvp0kcq",
    "name": null,
    "price": 223,
    "quantity": 1,
    "orderId": "cla7dmh5v000ivprghh27g22z",
    "menuItemId": "cl9t45hry0010vpr9q3pjo2kc",
    "comments": null,
    "stock": null,
    "menuItem": {
      "id": "cl9t45hry0010vpr9q3pjo2kc",
      "image": "https://madre-cafe.com/wp-content/uploads/2021/11/logo-madre-cafe-header.svg",
      "name": "Hot Cakes Item #2",
      "description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
      "price": "223",
      "available": true,
      "menuCategoryId": "cl9t45hqu000nvpr9pwqkk3gj"
    }
  }
]

What I want to do is make a new object with for 3 "especiales Item #2" but separated. Something like:

[
  {
    "name": "Especiales Item #2",
    "description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
    "price": "134"
  },
  {
    "name": "Especiales Item #2",
    "description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
    "price": "134"
  },
  {
    "name": "Especiales Item #2",
    "description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
    "price": "134"
  }
]

CodePudding user response:

If you wish to flatten all menu items separately, you may loop through the array as follows.

const orderList = [
  {
    "id": "cla7dnfln000mvprgde8b9ovh",
    "name": null,
    "price": 134,
    "quantity": 3,
    "orderId": "cla7dmh5v000ivprghh27g22z",
    "menuItemId": "cl9t45hry002uvpr9sp9jxl1t",
    "comments": null,
    "stock": null,
    "menuItem": {
      "id": "cl9t45hry002uvpr9sp9jxl1t",
      "image": "https://madre-cafe.com/wp-content/uploads/2021/11/logo-madre-cafe-header.svg",
      "name": "Especiales Item #2",
      "description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
      "price": "134",
      "available": true,
      "menuCategoryId": "cl9t45hqu000tvpr9fjw67ype"
    }
  },
  {
    "id": "cla8ehie20000vpusxqvp0kcq",
    "name": null,
    "price": 223,
    "quantity": 1,
    "orderId": "cla7dmh5v000ivprghh27g22z",
    "menuItemId": "cl9t45hry0010vpr9q3pjo2kc",
    "comments": null,
    "stock": null,
    "menuItem": {
      "id": "cl9t45hry0010vpr9q3pjo2kc",
      "image": "https://madre-cafe.com/wp-content/uploads/2021/11/logo-madre-cafe-header.svg",
      "name": "Hot Cakes Item #2",
      "description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
      "price": "223",
      "available": true,
      "menuCategoryId": "cl9t45hqu000nvpr9pwqkk3gj"
    }
  }
]

const getMenuItems = (arr) => {
  const items = [];
  for (let i = 0; i < arr.length; i  ) {
    for (let j = 0; j < arr[i].quantity; j  ) {
      items.push(arr[i]);
    }
  }
  return items;
};

const menuItems = getMenuItems(orderList);

  • Related