简体   繁体   中英

search for matching values in two arrays with repeated values in javascript

I have two arrays:

a = [2,2,3,0,6]
b = [6,3,2,2,0]

I am trying use for loop to match values and get the index of a in a new array c. How can we do this? Notice that there are multiple values which match and so I think the previous match must be skipped.

This is a proposal which respects the last index and looks further.

How it works:

It uses Array#map for iterating array b with a callback. map gets an own this space with an really empty object Object.create(null) .

The callback has on parameter bb which is one element of `b.

Next is to find the element is in array a with a Array#indexOf and a fromIndex , based on the former searches. The former index is stored in the this object, as long as the result is not -1 , because this would reset the fromIndex to zero.

If there is no this[bb] or a falsy value of this[bb] take zero as fromIndex .

Later, a found index is incremented and stored in this[bb] .

At least, the index is returned.

 var a = [2, 2, 3, 0, 6], b = [6, 3, 2, 2, 0], c = b.map(function (bb) { var index = a.indexOf(bb, this[bb] || 0); if (index !== -1) { this[bb] = index + 1; } return index; }, Object.create(null)); console.log(c); 

Another solution could be first generate an object with all indices of a and use it in the iteration of b for returning the indices.

The example is a bit extended, to show what happen if there is no more than two indices (2) and one without being in a (7).

The content of aObj with all indices of a :

 { "0": [3], "2": [0, 1], "3": [2], "6": [4] } 

 var a = [2, 2, 3, 0, 6], b = [6, 3, 2, 2, 0, 7, 2], aObj = Object.create(null), c; a.forEach(function (aa, i) { aObj[aa] = aObj[aa] || []; aObj[aa].push(i); }); c = b.map(function (bb) { return aObj[bb] && aObj[bb].length ? aObj[bb].shift() : -1; }); console.log(c); 

If I understand correct.

let c = a.map(i => b.indexOf(i))

or

var c = a.map(function(i) { return b.indexOf(i); });

As far I Understand, You can try this:

var a = [2,2,3,0,6];
var b = [6,3,2,2,0];

var c = new Array();

for(i = 0; i < b.length; i++)
{
    for(j = 0; j < a.length; j++)
    {  
        if(b[i] === a[j] && c.indexOf(j) < 0)
        {
             c.push(j);
             break;
        }
    }
}

console.log(c); // [4, 2, 0, 1, 3]

FIDDLE DEMO HERE

loop .map function and check same value by indexOf

indexOf will return a number,representing the position where the specified search value occurs for the first time, or -1 if it never occurs

var arr = [];    
a.map(function(v){      
    if(b.indexOf(v) > -1){
        arr.push(v);
    }
});
console.log(arr);

try something like this

var a = [2,2,3,0,6];
var b = [6,3,2,2,0];

var arrayLength_a = a.length;
var arrayLength_b = b.length;
var new_array=[];

 for (var i = 0; i < arrayLength_a; i++)
 {
    for (var j = 0; j < arrayLength_b; j++) 
    {
       if (a[i] == b[j]) 
       {
          if(new_array.indexOf(a[i]) === -1)
          {  
             new_array.push(a[i]);
          }
       }     
    }
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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