-1

I have the following array, which each element contains a information that corresponds to a start value, and end value and an id (i.e. start 5, end 10 and the id being apples).

var fruits = [
    [5, 10, apples],
    [11, 15, oranges]
];

for (var i = 0; i < fruits.length; i++) {
    var lookup = [];
    lookup.push({
        'START': fruits[i][0],
        'END': fruits[i][1],
        'VALUE': fruits[i][2]
    });
}

At the moment I have a variable that I'd like to compare between a range of values within each object in the array. So If my variable contains the int value 6, I want it to return apples as its within the range of 5 (start) and 10 (end). Can anyone advise my on how I can achieve this?

Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
tba
  • 53
  • 6
  • i don't get it.. if you want to compare a value between 2 value you can use `if(valueA >= 5 && valueA <= 10)` – aswzen May 21 '15 at 12:08
  • How and at what place, do you want to pass your variable for range comparison and return corresponding object of fruit? The condition would not be much complicated, consider @aswzen comment. – Parkash Kumar May 21 '15 at 12:08
  • Directly under the for loop. So I was looking along the lines checking if 6 is between the 5 (first) and 10 (end). If(myvar >= fruits[i][0] && myvar <= fruits[i][1]){ return apples } – tba May 21 '15 at 12:15

1 Answers1

0

What you will want to use is Array.filter on your lookup array. It creates a new array that meets a condition.

function pick(value) {
    return lookup.filter(function(fruit) {
        /*
         fruit will be the value of each element in the array
         so when it's the first element fruit will be
         {
            "START": 5,
            "END": 10,
            "VALUE": "apples"
         }
         */
        return false //insert logic here
    });
}