根据多个属性/条件在数组中查找正确的对象

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

我有一个与第三方 API 交互的网站,该 API 将处理产品的运输费用。在我们的网站上,“productId”与他们目录中的“externalProductId”相匹配,但他们有多个 ProductID 条目,因为他们根据 ProductId 和重量在数据库中对不同的 ProductId 进行分类。因此,在我们的网站中,像“包”这样的产品有多个条目,因为根据输入的重量,“包”产品有多个条目。该网站也是国际性的,因此其重量以磅和公斤为单位列出。我们需要将我们的 ProductId 与他们的“externalProductId”相匹配,并从他们的目录中选择正确的产品,以便我们可以将其发送到他们的定价 API。我需要对客户从我们网站上挑选的每件产品进行此查找,并将其发送到托运人定价 API,以便他们可以看到运费估算。

另一个问题是所有产品的最小重量始终为 0,因此我也可以将逻辑放入数组搜索中,在其中我硬编码如何找到最大重量 >50 磅之间的产品,但是 <70lbs but tomorrow if they change the pricing / weight ranges for these it would not work so i would rather find the products in their catalog dynamically for the correct weight which match our productId and 'externalProductId' and finds the correct one out of the multiple matches that is in the weight range. Can someone point out what would be the most efficient way to search the catalog based on these multiple search criteria?

这是我们网站上的示例产品

  { 'productId' : 'BAG', 'weight': 60, 'unit':'lbs' }

他们的目录中有两种产品与 'externalProductId': 'BAG' 匹配,但我需要在他们的产品目录中找到正确的匹配产品,因为它也匹配重量范围。

{
    "productId": "BG2",
    "externalProductId": "BAG",
    "productName": "Bag",
    "weight": {
      "minimum": [
        {
          "minWeight": 0,
          "unit": "lbs"
        },
        {
          "minWeight": 0,
          "unit": "kgs"
        }
      ],
      "maximum": [
        {
          "maxWeight": 70,
          "unit": "lbs"
        },
        {
          "maxWeight": 32,
          "unit": "kgs"
        }
      ]
    }
  },

包含所有产品的第 3 方产品目录

[
  {
    "productId": "BG1",
    "externalProductId": "BAG",
    "productName": "Bag",
    "weight": {
      "minimum": [
        {
          "minWeight": 0,
          "unit": "lbs"
        },
        {
          "minWeight": 0,
          "unit": "kgs"
        }
      ],
      "maximum": [
        {
          "maxWeight": 50,
          "unit": "lbs"
        },
        {
          "maxWeight": 23,
          "unit": "kgs"
        }
      ]
    }
  },
  {
    "productId": "BG2",
    "externalProductId": "BAG",
    "productName": "Bag",
    "weight": {
      "minimum": [
        {
          "minWeight": 0,
          "unit": "lbs"
        },
        {
          "minWeight": 0,
          "unit": "kgs"
        }
      ],
      "maximum": [
        {
          "maxWeight": 70,
          "unit": "lbs"
        },
        {
          "maxWeight": 32,
          "unit": "kgs"
        }
      ]
    }
  },
  {
    "productId": "GF1",
    "externalProductId": "GLF",
    "productName": "Golf Bag",
    "weight": {
      "minimum": [
        {
          "minWeight": 0,
          "unit": "lbs"
        },
        {
          "minWeight": 0,
          "unit": "kgs"
        }
      ],
      "maximum": [
        {
          "maxWeight": 50,
          "unit": "lbs"
        },
        {
          "maxWeight": 23,
          "unit": "kgs"
        }
      ]
    }
  },
  {
    "productId": "GF2",
    "externalProductId": "GLF",
    "productName": "Golf Bag",
    "weight": {
      "minimum": [
        {
          "minWeight": 0,
          "unit": "lbs"
        },
        {
          "minWeight": 0,
          "unit": "kgs"
        }
      ],
      "maximum": [
        {
          "maxWeight": 70,
          "unit": "lbs"
        },
        {
          "maxWeight": 32,
          "unit": "kgs"
        }
      ]
    }
  },
  {
    "productId": "MI1",
    "externalProductId": "MUS",
    "productName": "Music Instrument",
    "weight": {
      "minimum": [
        {
          "minWeight": 0,
          "unit": "lbs"
        },
        {
          "minWeight": 0,
          "unit": "kgs"
        }
      ],
      "maximum": [
        {
          "maxWeight": 50,
          "unit": "lbs"
        },
        {
          "maxWeight": 23,
          "unit": "kgs"
        }
      ]
    }
  },
  {
    "productId": "MI2",
    "externalProductId": "MUS",
    "productName": "Music Instrument",
    "weight": {
      "minimum": [
        {
          "minWeight": 0,
          "unit": "lbs"
        },
        {
          "minWeight": 0,
          "unit": "kgs"
        }
      ],
      "maximum": [
        {
          "maxWeight": 70,
          "unit": "lbs"
        },
        {
          "maxWeight": 32,
          "unit": "kgs"
        }
      ]
    }
  }
]
javascript
1个回答
0
投票

如果您需要根据多个条件返回单个产品,您可以使用几种不同的数组方法轻松完成此操作。对于这个例子,

reduce
感觉最合适,因为它允许您在一个循环中处理所有逻辑,而不是单独执行每个细化步骤。

请参阅下面的脚本:

const findMatchingProducts = (externalProductId, maxProductWeight, productWeightUnit) => {
  const matchingWeightProducts = data.reduce((acc, curr) => {
    // filter out any products without a matching externalProductId
    if (curr.externalProductId !== externalProductId) {
      return acc;
    }
    
    const {
      weight: {
        minimum,
        maximum,
      },
    } = curr;
    
    const { minWeight } = minimum.find(min => min.unit === productWeightUnit) || {};
    const { maxWeight } = maximum.find(max => max.unit === productWeightUnit) || {};
    
    // filter out any products that don't fall within the min/max weight range
    const isAboveMinWeight = minWeight <= maxProductWeight;
    const isBelowMaxWeight = maxWeight >= maxProductWeight;
    
    // if the product falls within the min/max weight range, add it to the list of matched products
    if (isAboveMinWeight && isBelowMaxWeight) {
      acc.push(curr);
    }
    
    return acc;
  }, []);
  
  // return the list of matched products
  return matchingWeightProducts;
};

console.log(findMatchingProducts('BAG', 60, 'lbs'));

结果:

[
  {
    "productId": "BG2",
    "externalProductId": "BAG",
    "productName": "Bag",
    "weight": {
      "minimum": [
        {
          "minWeight": 0,
          "unit": "lbs"
        },
        {
          "minWeight": 0,
          "unit": "kgs"
        }
      ],
      "maximum": [
        {
          "maxWeight": 70,
          "unit": "lbs"
        },
        {
          "maxWeight": 32,
          "unit": "kgs"
        }
      ]
    }
  }
]

对于本例,

data
是从第三方目录返回的产品数组。在
findMatchingProducts
函数中,它执行以下操作:

  • 忽略任何没有匹配的产品
    externalProductId
  • 使用提供的重量计量单位获取任何匹配产品的最小/最大重量
  • 检查以确保匹配产品的最小和最大重量分别高于和低于提供的最大重量
  • 返回符合条件标准的产品

如果您总是只需要返回单个产品,则可能需要添加其他属性来进一步过滤结果集,或者您可以只选择第一个结果作为“最佳匹配”。

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