0

Below is a small sample of my table. I will be collecting user data, and returning a rate. Is there a library or module to do this kind of query, or do I have to write something manually? So if my user is 23, goodDriver, does not have home insurance, the rate should come back as 2.00. This needs to be on the client side and work in Chrome, FF, Safari, and IE 9. thanks

        var rateTable = [
        ["lowerAge", "upperAge", "goodDriver", "hasHomeInsurance",    "rate"],
        ["20",      "30",         true,         true,                1.00],
        ["20",      "30",         true,         false,               2.00],
        ["20",       "30",        false,        true,                3.00],
        ["20",       "30",        false,        false,               4.00]
    ]
2
  • 3
    You should use SQL or some database, if you want to store that data on a javascript array, then the structure should be different Commented Jul 15, 2014 at 17:18
  • needs to work on client side, on Chrome, FF, Safari and IE 9+. Commented Jul 15, 2014 at 20:33

2 Answers 2

1

You want to restructure your data so that you have an array of objects instead, which will make it a lot easier to run simple queries.

from

var rateTable = [
    ["lowerAge", "upperAge", "goodDriver", "hasHomeInsurance",    "rate"],
    ["20",      "30",         true,         true,                1.00],
    ["20",      "30",         true,         false,               2.00],
    ["20",       "30",        false,        true,                3.00],
    ["20",       "30",        false,        false,               4.00]
];

to

var rates = [{
    lowerAge: "20",
    upperAge: "30",
    goodDriver: true,
    hasHomeInsurance: true,
    rate: 1.00,
}, {
    lowerAge: "20",
    upperAge: "30",
    goodDriver: true,
    hasHomeInsurance: false,
    rate: 2.00,
}, {
    lowerAge: "20",
    upperAge: "30",
    goodDriver: false,
    hasHomeInsurance: true,
    rate: 3.00,
}, {
    lowerAge: "20",
    upperAge: "30",
    goodDriver: false,
    hasHomeInsurance: false,
    rate: 4.00,
}];

Here's a snippet to do the conversion, if you can't do it beforehand:

var rates = rateTable.slice(1).map(function (row) {
    return this.reduce(function (obj, key, index) {
        obj[key] = row[index];
        return obj;
    }, {});
}, rateTable[0]);

As for querying the data after conversion, here's an example:

function getRate(age, goodDriver, hasHomeInsurance) {
    var rate = rates.find(function (rate) {
        return age >= rate.lowerAge &&
            age <= rate.upperAge &&
            rate.goodDriver === goodDriver &&
            rate.hasHomeInsurance == hasHomeInsurance;
    });
    return rate && rate.rate;
}

Edit: I should add that Array.prototype.find isn't supported everywhere, but can be replaced by a simple loop among other things. See this question.

Sign up to request clarification or add additional context in comments.

Comments

0

As others suggested, I would strongly recommend you to use mySQL. However, you have a question that seemed interesting to me, so I tried to solve it.

This is not the most efficient way, but there you go.

You could go through each row in your array, and check that all the values correspond to the ones you want to check.

The function would work like this:

function getRate(){
    // get all the values
    var age = parseInt($('#age').val());
    var goodDriver = $('input[name="goodDriver"]:eq(1)').is(':checked');
    var insurance = $('input[name="insurance"]:eq(1)').is(':checked');

    // check each row for a match
    for(var i=0; i<rateTable.length; i++){
        var row = rateTable[i];
        // if one is found, return the rate
        if (row[0]<=age && row[1]>=age && row[2]==goodDriver && row[3]==insurance)
            return row[4];
    }

    // if no match was found
    return false;
}

JS Fiddle Demo

Note

I removed the quotes around age in your table, to compare them as integers.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.