在js中查找数组中重复项的索引

时间:2018-02-05 05:49:50

标签: javascript arrays

我有两个数组

arr1=[ 0, 1, 2, 0, 2 ];

arr2=[ 0, 0, 1, 2, 2 ];

我必须从arr1找到arr2的元素索引,输出数组必须像[0,3,1,2,4];

I have written a code但它适用于没有重复的数组

var index = [];
for (i = 0; i <= arr2.length - 1; i++) {
  index.push(arr1.indexOf(arr2[i]));
}

5 个答案:

答案 0 :(得分:3)

您需要在第一个索引之后搜索第二个元素,并且对所有重复元素搜索相同(对于索引之后的第三个元素)。您可以在Array#indexOf方法中指定fromIndex参数,以便在特定索引处开始搜索。

// use a reference object to specify from index for duplicate
var ref = {};

var index = [];

for (i = 0; i < arr2.length; i++) {
  // specify from index from object, if not found set as 0
  var i1 = arr1.indexOf(arr2[i], ref[arr2[i]] || 0);

  // push the index
  index.push(i1);

  // specify from index for current element
  ref[arr2[i]] = i1 + 1;
}

&#13;
&#13;
var ref = {};

var arr1 = [0, 1, 2, 0, 2],
  arr2 = [0, 0, 1, 2, 2];


var ref = {};

var index = [];

for (i = 0; i < arr2.length; i++) {
  var i1 = arr1.indexOf(arr2[i], ref[arr2[i]] || 0);
  index.push(i1);
  ref[arr2[i]] = i1 + 1;
}

console.log(index);
&#13;
&#13;
&#13;

使用Array#map方法生成索引数组。

var index = arr2.map(function(v, i) {
  // get the index of the element, where specify from index to
  // search after a certain index for repeating element
  var i1 = arr1.indexOf(v, this[v] || 0);

  // set reference of index 
  this[v] = i1 + 1;

  // return index
  return i1;
  // set this argument as an object for from index reference
}, {});

&#13;
&#13;
var arr1 = [0, 1, 2, 0, 2],
  arr2 = [0, 0, 1, 2, 2];

var index = arr2.map(function(v, i) {
  var i1 = arr1.indexOf(v, this[v] || 0);
  this[v] = i1 + 1;
  return i1;
}, {});

console.log(index);
&#13;
&#13;
&#13;

答案 1 :(得分:0)

您可以查看数组并将其映射到anther数组,将第一个实例设置为undefined。显然,如果未定义的是您可能想要搜索的值,则此功能无效。

var zz = arr1.map(val => {
  if (!val) return undefined
  let ind = arr2.indexOf(val)
  if (ind) arr2[ind] = undefined
  return ind
})

答案 2 :(得分:0)

你可以做的是迭代arr2并从arr1中保存找到的索引在变量中,如果arr2的元素等于arr2中的前一个元素,则从保存的索引+ 1进行比较,为此你可以使用indexOf方法的第二个参数

var duplicate =[ 0, 1, 2, 0, 2 ];
var newiter =[ 0, 0, 1, 2, 2 ];
var indexArray = []; //RESULT ARRAY

var newiter = newiter.sort(); //IN CASE newiter IS NOT SORTED

var i = -1;
for(var j = 0; j<newiter.length; j++) {

  // check if element from newiter is equal to previous , if not set i to -1
  if(j > 0 && newiter[j] != newiter[j-1]) {
    i = -1;
  }

  // get index from duplicate but start searching from i+1
  i = duplicate.indexOf(newiter[j], i+1);
  indexArray.push(i);
}

console.log(indexArray);

答案 3 :(得分:0)

如果您只有正数,请尝试此

var temp = arr1.slice(0); //Clone arr1 to a temp Arr

var index = [];

arr2.forEach(item => {
    let ind = temp.indexOf(item);
    index.push(ind);
    ind > -1 && (temp[ind] = -1);
})

console.log(index);

答案 4 :(得分:-1)

var arr1 = [ 0, 1, 2, 0, 2 ];
var arr2 = [ 0, 0, 1, 2, 2 ]
var index = [];
var hash = {};
for (i = 0; i < arr2.length; i++) {
  var ind_temp;
  if(arr2[i] in hash){
    //console.log("here");
    ind_temp = arr1.indexOf(arr2[i],hash[arr2[i]] + 1);
    index.push(ind_temp);
    hash[arr2[i]] = ind_temp;
  }
  else{
    ind_temp = arr1.indexOf(arr2[i]);
    index.push(ind_temp);
    hash[arr2[i]] = ind_temp;
  }
}

console.log(index);