javascript中类似Vlookup的函数
实际上,我已经用Vlookup在excel上制作了这个,但现在我正在网页上制作这个 我有一个输入框,用户将在其中输入值javascript中类似Vlookup的函数,javascript,web,vlookup,indexof,Javascript,Web,Vlookup,Indexof,实际上,我已经用Vlookup在excel上制作了这个,但现在我正在网页上制作这个 我有一个输入框,用户将在其中输入值 <input class="text" type="text" name="rawScore" onchange="calcpercentile()"> 我应该写什么代码,如果我写这样我就得到了 input value (rawScores) (percentile) 1 10 2 20 3
<input class="text" type="text" name="rawScore" onchange="calcpercentile()">
我应该写什么代码,如果我写这样我就得到了
input value
(rawScores) (percentile)
1 10
2 20
3 30
4 40
你的例子似乎错了。我希望分数1对应第10百分位,2&3对应第20百分位,4对应第30百分位 本质上,我认为您要做的是:找到第一个原始分数的数组索引,该索引大于输入值,并从百分位数数组返回相应的值 Javascript可能看起来像这样:
var百分位数=[10,20,30,40,50,60,70,80,90];
var rawScores=[1,3,5,7,10,12,18,25,27];
功能图(输入){
让index=rawScores.findIndex(rawScore=>rawScore>=input);
返回百分位数[指数];
}
控制台日志(地图(1));
控制台日志(地图(2));
控制台日志(地图(3));
控制台日志(地图(4))代码>您可以输入文本:1
跨度显示“10”
window.onload=function(){
变量百分位数=[0,10,20,30,40,50,60,70,80,90];
document.getElementById(“rawScore”).onchange=function(){
var index=document.getElementById(“rawScore”).value;
document.getElementById(“百分位”).innerHTML=百分位[索引];
}
}
当然,首先要对数据集进行排序
const arr = [0,2,5,2,7,3];
const data = arr.sort();
接下来可能有帮助的是,这个函数用于查找最接近的数字的索引
console.log(findClosestIndex([0, 1, 2, 3.5, 4.5, 5], 4));
// output: 3
console.log(findClosestIndex([0, 1, 2, 3.49, 4.5, 5], 4));
// output: 4
console.log(findClosestIndex([0, 1, 2, 3.49, 4.5, 5], 90));
// output: 5
console.log(findClosestIndex([0, 1, 2, 3.49, 4.5, 5], -1));
// output: 0
function findClosestIndex(arr, element) {
let from = 0, until = arr.length - 1
while (true) {
const cursor = Math.floor((from + until) / 2);
if (cursor === from) {
const diff1 = element - arr[from];
const diff2 = arr[until] - element;
return diff1 <= diff2 ? from : until;
}
const found = arr[cursor];
if (found === element) return cursor;
if (found > element) {
until = cursor;
} else if (found < element) {
from = cursor;
}
}
}
将这个百分比转换成一个百分位数是一个四舍五入的问题
const percentile = (Math.floor(pct/10)+1) * 10;
(注:当股票的当前价格处于每日交易价格的某个百分位数时,我使用此功能买卖股票。)为什么原始分数4对应于第40个百分位数?您尝试了什么?添加您的calcpercentile实现。是的,这正是我想要的,但我是一个天真的人,仍然陷入了一些我没有得到结果的事情中…………我试图在codepen上复制解决方案,但仍然陷入困境
console.log(findClosestIndex([0, 1, 2, 3.5, 4.5, 5], 4));
// output: 3
console.log(findClosestIndex([0, 1, 2, 3.49, 4.5, 5], 4));
// output: 4
console.log(findClosestIndex([0, 1, 2, 3.49, 4.5, 5], 90));
// output: 5
console.log(findClosestIndex([0, 1, 2, 3.49, 4.5, 5], -1));
// output: 0
function findClosestIndex(arr, element) {
let from = 0, until = arr.length - 1
while (true) {
const cursor = Math.floor((from + until) / 2);
if (cursor === from) {
const diff1 = element - arr[from];
const diff2 = arr[until] - element;
return diff1 <= diff2 ? from : until;
}
const found = arr[cursor];
if (found === element) return cursor;
if (found > element) {
until = cursor;
} else if (found < element) {
from = cursor;
}
}
}
const index = findClosestIndex(data, input);
const pct = index / arr.length;
const percentile = (Math.floor(pct/10)+1) * 10;