You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何基于宽高参数在CSV数组中匹配对应价格?

实现思路与代码示例

首先,我们需要把你提供的原始字符串数组转换成结构化的键值映射,这样就能快速根据用户输入的宽高查询价格。下面是具体的步骤和代码实现:

1. 预处理原始数据

先把原始的字符串数组拆分成二维数组,并提取出宽度列和高度行的数值,同时构建高度到对应价格的映射:

// 你的原始价格数组
const priceRaw = [ ', 600, 900, 1200, 1500, 1800, 2100, 2400, 2700, 3000', '450, €89.80, €106.26, €122.72, €139.18, €155.64, €172.10, €188.56, €205.03, €221.49', '650, €102.97, €124.04, €145.10, €166.17, €187.23, €208.30, €229.36, €250.42, €271.49', '850, €116.15, €141.82, €167.49, €193.15, €218.82, €244.49, €270.15, €295.82, €321.49', '1050, €129.33, €159.60, €189.87, €220.14, €250.41, €280.68, €310.95, undefined, undefined', '1200, €139.21, €172.93, €206.65, €240.38, €274.10, €307.82, undefined, undefined, undefined', '1400, €152.39, €190.71, €229.04, €267.36, €305.69, undefined, undefined, undefined, undefined', '1600, €165.56, €208.49, €251.42, €294.35, undefined, undefined, undefined, undefined, undefined', '1800, €178.74, €226.27, €273.80, undefined, undefined, undefined, undefined, undefined, undefined', '2000, €191.92, €244.05, €296.19, undefined, undefined, undefined, undefined, undefined, undefined', '2200, €205.09, €261.83, undefined, undefined, undefined, undefined, undefined, undefined, undefined', '2400, €218.27, €279.61, undefined, undefined, undefined, undefined, undefined, undefined, undefined', '2600, €231.45, €297.39, undefined, undefined, undefined, undefined, undefined, undefined, undefined', '2800, €244.62, €315.17, undefined, undefined, undefined, undefined, undefined, undefined, undefined', '3000, €257.80, €332.95, undefined, undefined, undefined, undefined, undefined, undefined, undefined', '3200, €270.98, €350.73, undefined, undefined, undefined, undefined, undefined, undefined, undefined' ];

// 拆分每一行,去除多余空格
const priceMatrix = priceRaw.map(row => row.split(',').map(item => item.trim()));

// 提取宽度列(第一行,从第二个元素开始转成数字)
const widths = priceMatrix[0].slice(1).map(Number);

// 构建高度到价格数组的映射
const heightPriceMap = {};
for (let i = 1; i < priceMatrix.length; i++) {
  const height = Number(priceMatrix[i][0]);
  const prices = priceMatrix[i].slice(1);
  heightPriceMap[height] = prices;
}

2. 实现查询函数

接下来写一个函数,接收用户输入的inputHeight和inputWidth,返回对应的价格。这里按照你例子中的逻辑(输入宽度660匹配900列),采用取不小于输入值的最小宽/高的规则:

function getPrice(inputHeight, inputWidth) {
  // 第一步:找到匹配的高度行
  const availableHeights = Object.keys(heightPriceMap).map(Number);
  // 找到不小于输入高度的最小高度
  const matchedHeight = availableHeights.find(h => h >= inputHeight) || availableHeights[availableHeights.length - 1];
  if (!matchedHeight) return '暂无对应价格';
  
  const prices = heightPriceMap[matchedHeight];
  
  // 第二步:找到匹配的宽度列
  // 找到不小于输入宽度的最小宽度
  const matchedWidthIndex = widths.findIndex(w => w >= inputWidth);
  if (matchedWidthIndex === -1) return '暂无对应价格';
  
  const price = prices[matchedWidthIndex];
  return price === 'undefined' ? '暂无对应价格' : price;
}

3. 测试你的例子

用你给出的测试场景验证(这里推测你例子里的高度输入可能是笔误,实际应为650):

// 输入高度650,宽度660
console.log(getPrice(650, 660)); // 输出 €124.04,和你的例子一致

额外优化点

  • 如果需求是取不大于输入值的最大宽/高,只需要把find改成findLast,findIndex改成findLastIndex即可
  • 可以给用户添加输入范围提示,比如告知允许的宽高范围
  • 如果是后端实现,逻辑完全通用,只需要把语法转换成对应语言(Python、PHP等)即可

内容的提问来源于stack exchange,提问作者user18252288

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 13:02:46