访问查找表元素中的数据

Accessing data within elements of a lookup table

本文关键字:数据 元素 查找 访问      更新时间:2023-09-26

我有以下数组,其中每个元素包含一个信息,对应于一个开始值,结束值和一个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]
    });
}

现在我有一个变量,我想在数组中每个对象的值范围之间进行比较。因此,如果我的变量包含整型值6,我希望它返回苹果,因为它在5(开始)和10(结束)的范围内。谁能告诉我如何才能做到这一点?

您要使用的是Array。对查找数组进行筛选。它创建一个满足条件的新数组。

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