获取两个数组中相似值的索引

getting the index of the similar values of 2 arrays

本文关键字:相似 索引 数组 两个 获取      更新时间:2023-09-26

我想获得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

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)


移除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]
<<p> 更新演示/strong>

尝试使用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);
});
演示

循环遍历数组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]));
}

如果您碰巧需要索引列表和交集,您可以将交集存储在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);
注意:对于非常大的数组,这里的性能可能不是最优的。

对于已有的答案,我建议:

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小提琴演示实验和开发。

引用:

  • Array.prototype.every() .
  • Array.prototype.indexOf() .
  • Array.prototype.map() .
  • Object.prototype.constructor .