访问查找元素中的数据 table

Accessing data within elements of a lookup table

我有以下数组,其中每个元素都包含一个信息,对应于起始值、结束值和 id(即开始 5,结束 10,id 是苹果)。

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

目前我有一个变量,我想比较数组中每个对象的一系列值。因此,如果我的变量包含 int 值 6,我希望它在 5(开始)和 10(结束)的范围内 return apples。谁能告诉我如何实现这一目标?

您要使用的是查找数组中的 Array.filter。它创建一个满足条件的新数组。

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