0

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.

3
  • 7
    please add the wanted result. Commented May 19, 2016 at 9:57
  • 2
    a.map(function(item){ return b.indexOf(item); }) Commented May 19, 2016 at 9:59
  • 1
    ans wanted is: c = [4, 2, 0, 1, 3] Commented May 19, 2016 at 10:27

5 Answers 5

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

the ans is what I was looking for but it isn't working somehow in my editor. something i must add? like other functions or something like that?? could you simplify the meaning of this code or explain a bit?
0

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

3 Comments

the answer i was looking for is: c = [4, 2, 0, 1, 3]
thanks, the answer is correct but it shows nothing in fiddle and also in my editor
@ubuntuser, check the browser console.
0

If I understand correct.

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

or

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

1 Comment

Please provide a ES5 answer, since this question isn't tagged with ES6.
0

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

1 Comment

as i understood this gives the same array a: arr = c = [2,2,3,0,6], I was looking for c: [4, 2, 0, 1, 3]
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.