简体   繁体   English

如何获取Array中所有相同值的索引?

[英]How to get index of all occurrence of the same value in an Array?

I have two arrays like arr1, arr2. 我有两个数组,如arr1,arr2。 Each array having different kind of values. 每个数组都有不同类型的值。 In array one i have repeated some values in multiple times. 在数组一中,我多次重复一些值。 How can i get same value index and value? 我怎样才能获得相同的价值指数和价值? Also, how to get array two values from array one same value index? 另外,如何从数组中获取数组的两个值相同的值索引?

var arr1 = [100, 200, 100, 300, 600, 200];
var arr2 = [1, 2, 3, 4, 5, 6];

For example, If my custom value is 200. arr1 result will be [1, 5]. 例如,如果我的自定义值为200.arr1结果将为[1,5]。 How to get arr2 value like [2, 6]. 如何获得像[2,6]这样的arr2值。

Something like this: 像这样的东西:

var arr1 = [100, 200, 100, 300, 600, 200];
var arr2 = [1, 2, 3, 4, 5, 6];

var _arr1 = [];
var _arr2 = [];

for( var i = arr1.indexOf(200); i >= 0; i = arr1.indexOf(200,i+1) ) {
  _arr1.push(i);
  _arr2.push( arr2[i] );
}

console.log( _arr1, _arr2 );

I suggest to use Array#filter with the target array and return true (later also the value) if the element of array1 with the same index is equal to the search value. 我建议对目标数组使用Array#filter ,如果具有相同索引的array1的元素等于搜索值,则返回true (稍后也是值)。

The filter() method creates a new array with all elements that pass the test implemented by the provided function. filter()方法创建一个新数组,其中包含所有传递由提供的函数实现的测试的元素。

 var arr1 = [100, 200, 100, 300, 600, 200], arr2 = [1, 2, 3, 4, 5, 6]; function getMappedValues(array1, array2, value) { return array2.filter(function (_, i) { return array1[i] === value; }); } document.write('<pre>' + JSON.stringify(getMappedValues(arr1, arr2, 200), 0, 4) + '</pre>'); 

var arr1 = [100, 200, 100, 300, 600, 200];
var obj={};
for(var iloop=0; iloop< arr1.length; iloop++){
   if(obj[ arr1[iloop] ]){
     obj[ arr1[iloop] ].push(iloop) ;
   }else{
     obj[ arr1[iloop] ]  = [iloop];
   }
}

for(var k in obj){
  if(obj[k] && obj[k].length>1){
    console.log(k, obj[k])
  }
}

try 尝试

  function findIndexes(arr, value, fromIndex, indexes)
    {
       indexes = indexes || [];
       var index = arr.indexOf(value, fromIndex+1);
       console.log(index);
       if ( index != -1)
       {
          indexes.push(index);
          findIndexes(arr, value, index, indexes)
       }
    }
    var arr1 = [100, 200, 100, 300, 600, 200];
    var arr2 = [];
    var value = 200;
    findIndexes(arr1, value, -1, arr2);
    document.write(arr2);

One way of doing this is go through the array 1 and once find your custom value 一种方法是通过数组1并找到您的自定义值

  • Put that index into one array 将该索引放入一个数组中
  • Get value from array 2 in that index and put it to another array 从该索引中的数组2获取值并将其放入另一个数组

Here is the code 这是代码

    var arr1 = [100, 200, 100, 300, 600, 200];
    var arr2 = [1, 2, 3, 4, 5, 6];
    var index=[];
    var result=[];

    var customValue=200;

    for(var i=0;i<arr1.length;i++){
      if(arr1[i]==customValue){
        result.push(arr2[i]);
        index.push(i);
      }
    }
    console.log(index);
    console.log(result);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM