Lookup/Filter on Papa Parse data

1.8k views Asked by At

I need to do essentially a Vlookup (excel) or filter (if n > 1) on the data that I pulled from PapaParse.

// I know this is not asynchronous...
function filteredData(err,filterkey,data,wantedColumn) {
    if (err) console.error(error);
    t = [];
    for (i=0;i< data.length;i++;) {
        if (data[i][0] == filterkey) t[t.length] = data[i][wantedColumn];
    }
    return t;
}

Is there a quicker better way than going through all data everytime that I want to check on a key? My data is sorted in column[0] on my key. (There must be many easier ways, but is there a built in way?)

I parsed my data with headers so I have an array of objects rather than just an array (but I could change that it's not essential for my use case).

1

There are 1 answers

0
Reenen On

So from this I got "just hash it". My implementation looks a bit different (and is maybe slower because I don't understand that one that well). But after parsing, I hash the lookup "key" (in my case column[0]) Since I know that my skus are unique my code can be this simple (I actually have a header line).

skudata = baby.parse(data,{header:false});
for (i=1;i<skudata.data.length;i++){
    skuhash[skudata.data[i][0]] = i;
}

Then when I want to filter/lookup I just do this:

if (skuhash.hasOwnProperty(sku)) {
    skuloc = skuhash[sku];
    if (skudata.data[skuloc][col] == key) { return true;}
}