jquery - 获取2个数组的类似值的索引

时间:2015-08-22 14:00:16

标签: javascript jquery

我想得到array1和array2的类似值的索引,并将其存储到 stored_index变量中。

array1 = ["50","51","52","53","54","55","56","57","58","59"];

array2 = ["59","55","51"];

存储的索引应该如下所示。

stored_index = [9,5,1]; // index 9,5,1 is equal to their indices in array 1

5 个答案:

答案 0 :(得分:1)

Javascript map()可以执行此操作:

var array1 = ["50","51","52","53","54","55","56","57","58","59"];
var array2 = ["59","55","51"];
var stored_index = array2.map(function(val){
    return array1.indexOf(val)
}).filter(function(val){
    return (val != -1)
});

console.log(stored_index)

DEMO

删除filter

var array1 = ["50","51","52","53","54","55","56","57","58","59"];
var array2 = ["59","55","51"];
var stored_index = array2.map(function(val){
    return (array1.indexOf(val) != -1) ? array1.indexOf(val) : null;
});

console.log(stored_index)//output [9, 5, 1]

Updated DEMO

答案 1 :(得分:0)

尝试使用Array.prototype.map()Array.prototype.indexOf()来实现您的目标,

var array1 = ["50","51","52","53","54","55","56","57","58","59"];
var array2 = ["59","55","51"];
var stored_index = array2.map(function(val){
  return array1.indexOf(val);
});

DEMO

答案 2 :(得分:0)

只需遍历数组2并使用IndexOf函数

var array1 = ["50","51","52","53","54","55","56","57","58","59"];
var array2 = ["59","55","51"];
var output = [];
for(var item in array2){
  output.push(array1.indexOf(array2[item]));
}

答案 3 :(得分:0)

如果您需要索引列表和交集点,您可以将交集存储在a3数组中,然后将其编入索引。

var array1 = ["50", "51", "52", "53", "54", "55", "56", "57", "58", "59"];
var array2 = ["59", "55", "51"];
var a3 = array1.filter(function (n) {
    return array2.indexOf(n) !== -1
});

alert(a3);
stored_index = [];
var i = 0;
for (; i < a3.length; i++) {
    stored_index.push(array1.indexOf(a3[i]));
}
alert(stored_index);

注意:对于非常大的阵列,这里的性能可能不是最佳的。

答案 4 :(得分:0)

根据现有答案,我建议:

var array1 = ["50", "51", "52", "53", "54", "55", "56", "57", "58", "59"],
    array2 = ["59", "55", "51"];

// named function, to which arguments are passed:    
function getIndicesOfCommonArrayValues(one, two) {

    // if the named arguments both match the
    // assessment within the Array.prototype.every()
    // anonymous function we proceed, otherwise we
    // do nothing; if every assessment performed
    // returns true, then the method itself returns
    // a Boolean true:
    if ([one, two].every(function (arr) {
        // 'arr' is the current array-value of the array
        // over which we're iterating:

        // here check that the typeof arr is not 'undefined', AND
        // that the constructor of arr is Array; this ensures that
        // we have two defined array-values, and that the array
        // value we're testing is itself an Array:
        return 'undefined' !== typeof arr && arr.constructor === Array;
    })) {

        // here we find the longest array:
        var longestArray = one.length >= two.length ? one : two,
        // here we find the shortest array:
            shortestArray = one.length < two.length ? one : two;

        // we return the array returned by Array.prototype.map(),
        // which iterates over the shortest array:
        return shortestArray.map(function (value) {
            // 'value' is the current array-value of 
            // the array over which we're iterating.

            // if the longestArray contains the current
            // array-value:
            if (longestArray.indexOf(value) > -1) {

                // we return the index of that array
                // value to the array we're creating
                // Array.prototype.map():
                return longestArray.indexOf(value);
            }
        });
    }
}

console.log(getIndicesOfCommonArrayValues(array1, array2));

var array1 = ["50", "51", "52", "53", "54", "55", "56", "57", "58", "59"],
    array2 = ["59", "55", "51"];

function getIndicesOfCommonArrayValues(one, two) {
    if ([one, two].every(function (arr) {
        return 'undefined' !== typeof arr && arr.constructor === Array;
    })) {
        var longestArray = one.length >= two.length ? one : two,
            shortestArray = one.length < two.length ? one : two;
        
        return shortestArray.map(function (value) {
            if (longestArray.indexOf(value) > -1) {
                return longestArray.indexOf(value);
            }
        });
    }
}

console.log(getIndicesOfCommonArrayValues(array1, array2));

外部JS Fiddle demo用于实验和开发。

参考文献: