文件里面有俩个数据data1和data2 逗号前面为data1 逗号后面为data2 俩个值是对应 帮我写一个js方法 传入data2,方法根据data2的范围 返回data1
时间: 2024-11-09 15:21:02 浏览: 7
根据您提供的文档内容,可以编写一个JavaScript函数来实现您的需求。该函数将接收 `data2` 作为参数,并返回对应的 `data1` 值。以下是实现代码:
```javascript
function findData1(data2) {
// 文档中的数据对
const dataPairs = [
[-45, 247700], [-44, 233350], [-43, 219950], [-42, 207430], [-41, 195720], [-40, 188500],
[-39, 177962], [-38, 168096], [-37, 158853], [-36, 150190], [-35, 144100], [-34, 136329],
[-33, 129036], [-32, 122190], [-31, 115759], [-30, 111300], [-29, 105501], [-28, 100049],
[-27, 94919], [-26, 90090], [-25, 86430], [-24, 82081], [-23, 77984], [-22, 74122],
[-21, 70479], [-20, 67770], [-19, 64472], [-18, 61358], [-17, 58418], [-16, 55640],
[-15, 53410], [-14, 50897], [-13, 48520], [-12, 46272], [-11, 44143], [-10, 42470],
[-9, 40534], [-8, 38701], [-7, 36963], [-6, 35316], [-5, 33900], [-4, 32405],
[-3, 30987], [-2, 29641], [-1, 28363], [0, 27280], [1, 26114], [2, 25007],
[3, 23953], [4, 22952], [5, 22050], [6, 21138], [7, 20270], [8, 19443],
[9, 18656], [10, 17960], [11, 17239], [12, 16552], [13, 15896], [14, 15271],
[15, 14690], [16, 14118], [17, 13573], [18, 13053], [19, 12555], [20, 12090],
[21, 11633], [22, 11197], [23, 10780], [24, 10381], [25, 10000], [26, 9632],
[27, 9280], [28, 8944], [29, 8621], [30, 8313], [31, 8008], [32, 7724],
[33, 7451], [34, 7190], [35, 6940], [36, 6695], [37, 6464], [38, 6243],
[39, 6030], [40, 5827], [41, 5620], [42, 5432], [43, 5251], [44, 5077],
[45, 4911], [46, 4742], [47, 4588], [48, 4440], [49, 4297], [50, 4160],
[51, 4016], [52, 3889], [53, 3767], [54, 3649], [55, 3536], [56, 3418],
[57, 3313], [58, 3211], [59, 3114], [60, 3020], [61, 2919], [62, 2832],
[63, 2747], [64, 2666], [65, 2588], [66, 2505], [67, 2432], [68, 2361],
[69, 2293], [70, 2228], [71, 2156], [72, 2095], [73, 2036], [74, 1979],
[75, 1924], [76, 1864], [77, 1812], [78, 1762], [79, 1714], [80, 1668],
[81, 1617], [82, 1573], [83, 1531], [84, 1490], [85, 1451], [86, 1411],
[87, 1373], [88, 1336], [89, 1300], [90, 1268], [91, 1232], [92, 1200],
[93, 1166], [94, 1137], [95, 1108], [96, 1079], [97, 1051], [98, 1024],
[99, 998]
];
// 查找与给定 data2 最接近的数据对
let closestPair = null;
let minDiff = Infinity;
for (let i = 0; i < dataPairs.length; i++) {
const diff = Math.abs(dataPairs[i][1] - data2);
if (diff < minDiff) {
minDiff = diff;
closestPair = dataPairs[i];
}
}
return closestPair[0];
}
// 示例用法
const data2 = 27280;
const result = findData1(data2);
console.log(result); // 输出: -1
```
这个函数 `findData1` 接收 `data2` 作为参数,并在 `dataPairs` 数组中查找最接近 `data2` 的数据对,然后返回对应的 `data1` 值。您可以根据需要调用此函数并传入不同的 `data2` 值来获取相应的 `data1` 值。
阅读全文