通过另一个对象数组的方法过滤对象数组

时间:2021-07-25 06:11:48

标签: javascript arrays object filter

我正在制作一个送餐应用程序,我有两个不同的对象数组。其中一个是cartItems,另一个是foodItems。数组可以是不同的大小。那么我想遍历每个数组并检查什么

  1. 如果两个数组的 id 匹配。

  2. Note 我想检查数量是否存在,然后按新数量增加它,否则只需添加一个新数量

  3. 检查foodItems数组中是否存在itemDetails,如果存在,检查cartItems的price是否与那个foodItem匹配,然后更新cartItems对象,否则删除它们.

  4. 如果 itemDetails 不存在,则更新项目的数量。

更新 如果有两个id和价格相近的商品,需要添加数量

这是我的cartItems

let cartItems = [
  { id: 1, price: 120, quantity: 7 },
  { id: 2, price: 70, quantity: 4 },
  { id: 1, price: 70, quantity: 3 },
  { id: 3, price: 60, quantity: 1 },
  {id: 1, price: 120, quantity: 2}
];

这是我的foodItems

let foodItems = [
{
    id: 1,
    name: "chicken",
    itemDetails: [
   { 
    price: 120, 
    details: "2 pcs of chicken biryani"
   }, 
    {
     price: 70, 
     details: "1 pcs of chicken biryani"
     }
    ],
  },
  {
    id: 2,
    name: "Mutton",
    itemDetails: [
      {
        price: 120,
        details: "Two pieces of mutton biryani",
      },
     {
      price: 70,
      details: "one pcs of mutton biryani" 
     },
    ],
  },
  

  { id: 3, name: "Ice Cream", price: 60 },
];

这是我想要的输出

let filteredArrayOuput = [
  {
    id: 1,
    name: "Chicken Biryani",
    itemDetails: [
      {
        price: 120,
        details: "Two pieces of chicken Biryani",
      },
    ],
    quantity: 7,
  },
  {
    id: 2,
    name: "Mutton Biryani",
    itemDetails: [
      {
        price: 70,
        details: "Two pieces of mutton biryani",
      },
    ],
    quantity: 4,
  },
  {
    id: 1,
    price: "Chicken Biryani",
    quantity: 3,
    itemDetails: [
      {
        price: 70,
        details: "Two pieces of Chicken Biryani",
      },
    ],
  },
  { id: 3, price: 60, quantity: 1 },
];

这就是我迄今为止所做的

const filterFunc = (arr, price) => {
  let filtered = arr.filter((item) => {
    return item.price == price;
  });
  return filtered;
};

const filterArray = (arr1, arr2) => {
  const filtered = arr2.filter((el) => {
    let arr = arr1.find(({ id, quantity, price }) => {
      if (el.id === id) {
         if (el.itemDetails !== undefined && el.itemDetails.length !== 0) {
           let itemDetails = el.itemDetails;
           return (
             (el.quantity = quantity),
             (el.itemDetails = filterFunc(itemDetails, price))
           );
         } else {
           return (el.quantity = quantity);
         }
      }
    });

    return arr;
  });
  return filtered;
};
console.log(filterArray(cartItems, foodItems))

1 个答案:

答案 0 :(得分:1)

您可以查看以下代码。

  1. 从 FoodItems 数组中查找 existingFoodItem
  2. 通过比较价格找到priceObj
  3. 如果 itemDetails 存在,则返回带有价格详细信息的新对象(使用 ? 检查),否则如果不存在 itemDetails 则不返回价格。

let cartItems = [
      { id: 1, price: 120, quantity: 7 },
      { id: 1, price: 120, quantity: 1 },
      { id: 2, price: 70, quantity: 4 },
      { id: 1, price: 70, quantity: 3 },
      { id: 3, price: 60, quantity: 1 },
    ];

    let foodItems = [
    {
        id: 1,
        name: "chicken",
        itemDetails: [
       { 
        price: 120, 
        details: "2 pcs of chicken biryani"
       }, 
        {
         price: 70, 
         details: "1 pcs of chicken biryani"
         }
        ],
      },
      {
        id: 2,
        name: "Mutton",
        itemDetails: [
          {
            price: 120,
            details: "Two pieces of mutton biryani",
          },
         {
          price: 70,
          details: "one pcs of mutton biryani" 
         },
        ],
      },
      

      { id: 3, name: "Ice Cream", price: 60 },
    ];


    let result = [];
    
    cartItems.forEach(cart => {
      let esitingItem = result.find(r => r.id === cart.id && r.itemDetails.find(i => i.price === cart.price));
      if(esitingItem){
       esitingItem.quantity += cart.quantity;
       return;
      }
      let existingFoodItem = foodItems.find(food => food.id === cart.id);
      if(existingFoodItem){
        let priceObj = existingFoodItem.itemDetails?.find(item => item.price === cart.price);
        if(priceObj){
          result.push({id:cart.id,name:existingFoodItem.name,itemDetails:[{...priceObj}],quantity:cart.quantity});
        }
        else{
          return result.push({id:cart.id,name:existingFoodItem.name,quantity:cart.quantity});
        }
      }
    });


    console.log(result);

相关问题