3

Using this code...

var a = ['volvo','random data'];
var b = ['random data'];
var unique = $.grep(a, function(element) {
    return $.inArray(element, b) == -1;
});

var result = unique ;

alert(result); 

...I am able to find which element of Array "a" is not in Array "b".

Now I need to find:

  • if an element of Array "a" is in Array "b"
  • what is its index in Array "b"

For example "Random data" is in both arrays, so I need to return its position in Array b which is zero index.

OzrenTkalcecKrznaric
  • 5,535
  • 4
  • 34
  • 57
user2137186
  • 817
  • 6
  • 22
  • 39

5 Answers5

6

Regarding your comment, here is a solution:

with jQuery:

$.each( a, function( key, value ) {
    var index = $.inArray( value, b );
    if( index != -1 ) {
        console.log( index );
    }
});

without jQuery:

a.forEach( function( value ) {
    if( b.indexOf( value ) != -1 ) {
       console.log( b.indexOf( value ) );
    }
});
Matthias Holdorf
  • 1,040
  • 1
  • 14
  • 19
  • 1
    By the way,`indexOf()` just returns the first occurence of that value. So unless all the values in the array are unique, in this case you will always get one position per value. Anyway, I am glad you found a solution to your problem :) – Vivek Pradhan Jul 25 '13 at 12:10
5

Convert both array to string and compare

if (JSON.stringify(a) == JSON.stringify(b))
{
    // your code here
}
Prince Prasad
  • 1,528
  • 1
  • 16
  • 20
1

You could just iterate over a and use Array.prototype.indexOf to get the index of the element in b, if indexOf returns -1 b does not contain the element of a.

var a = [...], b = [...]
a.forEach(function(el) {
    if(b.indexOf(el) > 0) console.log(b.indexOf(el));
    else console.log("b does not contain " + el);
});
user1983983
  • 4,793
  • 2
  • 15
  • 24
  • i know how to do it now... thanks for your help but if i start iterating over millions record it will take a lot of time... – user2137186 Jul 25 '13 at 11:54
1

This should probably work:

  var positions = [];
  for(var i=0;i<a.length;i++){
  var result = [];
       for(var j=0;j<b.length;j++){
          if(a[i] == b[j])
            result.push(i); 
  /*result array will have all the positions where a[i] is
    found in array b */
       }
  positions.push(result);
 /*For every i I update the required array into the final positions
   as I need this check for every element */ 
 }

So your final array would be something like:

  var positions = [[0,2],[1],[3]...] 
  //implies a[0] == b[0],b[2], a[1] == b[1] and so on.

Hope it helps

Vivek Pradhan
  • 4,777
  • 3
  • 26
  • 46
  • thanks for you help :) but i don't want to iterate over a million records – user2137186 Jul 25 '13 at 11:56
  • `array.indexOf(value)` doesn't return you the index in constant time. The function implicitly checks all the indices against the value and returns a response. So you will have to iterate over a million records if you have a million records because you are using an array. Had you used a different data structures, the search algorithms can run faster than **O(N)** time. – Vivek Pradhan Jul 25 '13 at 12:03
1

You can try this:

var a = ['volvo','random data'];
var b = ['random data'];
$.each(a,function(i,val){
var result=$.inArray(val,b);
if(result!=-1)
alert(result); 
})
L10
  • 389
  • 1
  • 11