简体   繁体   中英

Vlookup like function in javascript

Actually I have made this on excel using Vlookup but now I am making this on webpage.

I have a input box where user will enter the value

<input class="text" type="text" name="rawScore" onchange="calcpercentile()">

and I have a span of where user can get the result

<span id="percentile"></span>

I have two arrays

var percentile = [10, 20, 30, 40, 50, 60, 70, 80, 90];
var rawScores = [1, 3, 5, 7, 10, 12, 18, 25, 27];

what code should I write that if I write so I get the

input       value
(rawScores)  (percentile)
1            10
2            20
3            30
4            40

Your example seems wrong. I expect score 1 to map to the 10th percentile, 2 & 3 to the 20th percentile, and 4 to the 30th percentile.

In essence, I think what you're trying to do is: find the array index of the first raw score that is greater than the input, and return the corresponding value from the percentiles array.

The Javascript could look something like this:

 var percentiles = [10, 20, 30, 40, 50, 60, 70, 80, 90]; var rawScores = [1, 3, 5, 7, 10, 12, 18, 25, 27]; function map(input) { let index = rawScores.findIndex(rawScore => rawScore >= input); return percentiles[index]; } console.log(map(1)); console.log(map(2)); console.log(map(3)); console.log(map(4));

Note that browser support for Array#findIndex() is limited. If you need wide browser support, a simple loop-based approach might be better:

 var percentiles = [10, 20, 30, 40, 50, 60, 70, 80, 90]; var rawScores = [1, 3, 5, 7, 10, 12, 18, 25, 27]; function map(input) { for (var i = 0; i < rawScores.length; i++) { if (rawScores[i] >= input) { return percentiles[i]; } } } console.log(map(1)); console.log(map(2)); console.log(map(3)); console.log(map(4));

you can input text : 1

span display "10"

 window.onload = function(){ var percentile = [0,10, 20, 30, 40, 50, 60, 70, 80, 90]; document.getElementById("rawScore").onchange = function () { var index = document.getElementById("rawScore").value; document.getElementById("percentile").innerHTML = percentile[index]; } }
 <input class="text" type="text" id="rawScore"> <span id="percentile"></span>

First you sort your dataset of course

const arr = [0,2,5,2,7,3];
const data = arr.sort();

What may help next, is this function to find the index of the closest number.

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;
        }
    }
}

So, now you know your index and the length of your array. And you have to get a percentile from that. Let's first calculate an exact percentage.

const index = findClosestIndex(data, input);
const pct = index / arr.length;

Turning this percentage into a percentile is a matter of rounding it.

const percentile = (Math.floor(pct/10)+1) * 10;

(PS: I use this function for buying/selling stocks when their current price is in a certain percentile of the daily transaction price rates.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM