【问题标题】:Filtering out array of object and do maths for specific element过滤出对象数组并对特定元素进行数学运算
【发布时间】:2021-08-16 18:28:47
【问题描述】:

我有一个对象数组。比方说

let cartItems = [
   {title: "Veg Salad", category: {title: "Salad"}, quantity: 2, price: 30},
   {title: "Roti", category: {title: "Breads"}, quantity: 3, price: 10},
   {title: "Biriyani", category: {title: "Biriyani"}, quantity: 2, price: 100},
   {title: "Veg Curry", category: {title: "Curry"}, quantity: 3, price: 60},
]

这里 类别 - 沙拉和面包不收取包装费,但 类别 - Biriyani 和咖喱 收取 20 卢比的包装费。所以我想过滤掉其余的数组对象,每个数量增加 20 卢比,并想要总包装费。

const findPackCharge = cartItems.filter((ele) => {
  return (
    ele.category?.title != "Salad" && ele.category?.title != "Breads"
  );
});

console.log(findPackCharge, "FIND");

我尝试了很多,但无法弄清楚。

【问题讨论】:

  • 4个物品中的任何一个都没有包装费。
  • @Shubanker 上面写的很清楚,沙拉和面包类不收包装费,Biriyani 和咖喱类20卢比包装费
  • 循环过滤后的数组,计算20 * ele.quantity的总数

标签: javascript arrays object filter


【解决方案1】:

不确定您要做什么,但如果只是要获得总包装费,只需将每个项目的数量乘以 20 并保存总和。

const cartItems = [
   {title: "Veg Salad", category: {title: "Salad"}, quantity: 2, price: 30},
   {title: "Roti", category: {title: "Breads"}, quantity: 3, price: 10},
   {title: "Biriyani", category: {title: "Biriyani"}, quantity: 2, price: 100},
   {title: "Veg Curry", category: {title: "Curry"}, quantity: 3, price: 60},
]

const NO_PACKING_CHARGE_ITEMS = ["Breads", "Salad"];
const totalPackingCharge = cartItems.reduce((accu, item) => {
  const isNoPackingChargeItem = NO_PACKING_CHARGE_ITEMS.includes(item.category.title);
  return isNoPackingChargeItem ? accu + (item.quantity * 20) : accu;
}, 0);
console.log(totalPackingCharge);

【讨论】:

    【解决方案2】:

    使用reduce() 计算总包装费用。在回调函数中可以跳过不收费的项目。

    let cartItems = [
       {title: "Veg Salad", category: {title: "Salad"}, quantity: 2, price: 30},
       {title: "Roti", category: {title: "Breads"}, quantity: 3, price: 10},
       {title: "Biriyani", category: {title: "Biriyani"}, quantity: 2, price: 100},
       {title: "Veg Curry", category: {title: "Curry"}, quantity: 3, price: 60},
    ];
    
    const findPackCharge = cartItems.reduce((total, ele) => {
      if (ele.category?.title != "Salad" && ele.category?.title != "Breads") {
        total += 20 * ele.quantity;
      }
      return total;
    }, 0);
    
    console.log(findPackCharge, "FIND");

    【讨论】:

      【解决方案3】:

      const cartItems = [
          { title: "Veg Salad", category: { title: "Salad" }, quantity: 2,  price: 30 },
          { title: "Roti", category: { title: "Breads" }, quantity: 3, price: 10 },
          { title: "Biriyani", category: { title: "Biriyani" }, quantity: 2, price: 100 },
          { title: "Veg Curry", category: { title: "Curry" }, quantity: 3, price: 60 }
      ];
      
      const packingFee = 20;
      const noPackingItems = ["Breads", "Salad"];
      const findPackCharge = cartItems.filter((ele) =>         
          !noPackingItems.includes(ele.category?.title)
        ).reduce((total, e) => total + e.quantity * packingFee, 0) ;
      
      console.log(findPackCharge, "for packing charge");

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-28
        • 2015-08-31
        • 2020-06-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多