使用 javascript 根据数组中的数量过滤所有大于最近的项目

问题描述 投票:0回答:1

三天前我有一个问题并得到了帮助。今天我有一个新问题。我有一系列基于产品数量的促销条件。如果用户订购的产品与产品id匹配并且大于或等于促销条件中的数量,则该订单应该为该用户打折。促销数据结构是

const promotions = [
   {
      id: 1,
      name: 'Promotion 1',
      conditionQuantity: 3,      // need product in order >= this quantity to discount order
      discount: 10000,
      conditionProducts: [
        { promotionId: 1, productId: 1 },
        { promotionId: 1, productId: 2 },
      ]
   },
   {
      id: 2,
      name: 'Promotion 1',
      conditionQuantity: 5,
      discount: 15000,
      conditionProducts: [
        { promotionId: 2, productId: 1 },
        { promotionId: 2, productId: 2 },
        { promotionId: 2, productId: 3 },
      ]
   },
   {
      id: 3,
      name: 'Promotion 2',
      conditionQuantity: 3,
      discount: 12000,
      conditionProducts: [
        { promotionId: 3, productId: 1 },
      ]
   }
]

订单产品结构为:

const productList = [
 {
  id: 1,             // product id
  name: 'Product 1',
  quantity: 3,       // quantity of product in order
 },
 {
  id: 2,
  name: 'Product 2',
  quantity: 6,
 },
 {
  id: 3,
  name: 'Product 3',
  quantity: 7,
 }
]

我期望我的案例结果是:

const result = [
  {
    id: 1,
    name: 'Promotion 1',
    totalDiscount: 10000,
  },
  {
    id: 2,
    name: 'Promotion 1',
    totalDiscount: 30000
  },
  {
    id: 3,
    name: 'Promotion 2',
    totalDiscount: 12000
  },
]

总折扣= 52000(因为

productId = 1
满足“促销1”和“促销2”(3 >= 3),
productId = 2
productId = 3
满足“促销2”的条件(6, 7 > 5) )

我尝试了这段代码,但没有得到正确的预期结果:

// get product condition in promotions
const getPromotionProducts = (promotions) => {
  return promotions.reduce((acc, promotion) => {
    if (promotion.conditionProducts.length > 0) {
      acc = acc.concat(promotion.conditionProducts)
    };
    return acc;
  }, []);
}

const getUserPromotions = (promotions, productList) => {
  const promotionProducts = getPromotionProducts(promotions);
  
  const mapPromotionConditionAndProductList = promotionProducts.reduce((acc, promotionProduct) => {
      const findProduct = productList.find(product => product.id === promotionProduct.productId);
      if (findProduct) acc = acc.concat(promotionProduct);
      return acc;
  }, []);

  let result = [], max = 0;
  for (const mappedProduct of mapPromotionConditionAndProductList) {
      
    const findPromotion = promotions.find(promotion => promotion.id === mappedProduct.promotionId);
    const findProduct = productList.find(product => product.id === mappedProduct.productId);

    if (findPromotion.conditionQuantity <= findProduct.quantity && findPromotion.conditionQuantity >= max) {
      if (findPromotion.conditionQuantity > max ) {
        max = findPromotion.conditionQuantity;
        result = [];
      }
      result.push({ ...findPromotion, productId: findProduct.id });
    }
  }
  return Object.values(result.reduce((acc, item) => {
    const key = `${item.id}-${item.promotionId}`;
    if (!acc[key]) { acc[key] = { ...item, totalDiscount: 0 } }
    acc[key].totalDiscount += item.discount;

    return acc;
  }, {}));
};

我的代码结果不正确 – 缺少带有

productId = 1
的“促销 1”和“促销 2”。

[
  {
    id: 2,
    name: 'Promotion 1',
    totalDiscount: 30000,
    productId: 2
  }
]

如何修复

getUserPromotions
函数以给出正确的响应?我期待得到任何人的帮助。

javascript arrays object reduce
1个回答
0
投票

我建议我的答案:

const promotions = [
   {
      id: 1,
      promotionId: 1,
      conditionQuantity: 3,
      conditionProducts: [1,2],
      
      // output: [1], 
   },
   {
      id: 2,
      promotionId: 1,
      conditionQuantity: 5,
      conditionProducts: [1,2,3],
      
      // output: [2, 3]
   },
   {
      id: 3,
      promotionId: 2,
      conditionQuantity: 3,
      conditionProducts: [1],
      
      // output: [1]
   }
]

const productList = [
 {
  id: 1,            
  quantity: 3,  
  
  // promotion : [1,3]
 },
 {
  id: 2,
  quantity: 6, 
  
  // promotion: [2]
 },
 {
  id: 3,
  quantity: 7,
  
  // promotion: [2]
 }
]


const sortedProductList = productList.sort((a,b)=>b.quantity - a.quantity);

const sortedPromotion = promotions.sort((a,b) => b.conditionQuantity -a.conditionQuantity );
]

//------


sortedProductList.forEach((product)=>{
    product.apply = [];
    product.promotionReal = [];
    
    sortedPromotion.forEach((promotion)=>{
        
        const ifContains = promotion.conditionProducts.includes(product.id);
        const ifOver = product.quantity >= promotion.conditionQuantity;
        const ifPreviousAdded = product.apply.includes(promotion.promotionId);
        
        if(ifContains && ifOver && !ifPreviousAdded){
            product.apply.push(promotion.promotionId);
            product.promotionReal.push(promotion.id);
        }
    });
});

© www.soinside.com 2019 - 2024. All rights reserved.