javascript - How to get index of all occurrence of the same value in an Array? - Stack Overflow

I have two arrays like arr1, arr2. Each array having different kind of values. In array one i have repe

I have two arrays like 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]. How to get arr2 value like [2, 6].

I have two arrays like 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]. How to get arr2 value like [2, 6].

Share Improve this question asked Mar 14, 2016 at 6:22 SathyaSathya 1,7343 gold badges38 silver badges59 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 2

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

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.

The filter() method creates a new array with all elements that pass the test implemented by the provided function.

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

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

  • Put that index into one array
  • Get value from array 2 in that index and put it to another array

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745363401a4624463.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信