0

Assume, we have two arrays,

 array1 = [1,2,3,6,3,5,2,5,2,4,3]
 array2 = [3,4,5]

How can I find the value "3" which resides inside array2 and then compare the same from array1 Any help is appreciated.

Thanks.

Abdul
  • 90
  • 1
  • 9
  • 1
    Please demonstrate some attempt at solving the problem yourself. – Tro Jul 31 '13 at 12:20
  • 1
    What exactly do you need? Do you need to know which elements are in both arrays or how often the values of array2 appear in array1? What are you expecting as output? – Lars Ebert Jul 31 '13 at 12:21
  • I have a set of checkboxes inside a ul li. While onchange the checbox, I getting all the check boxes id and store in a temp var., and also get the current checkbox value in another var. Now, I need to compare and remove the one which i selected currently, say for example.: I having some values in array1 as "1,0:2,0,3,0,4,0", next I clicking the checkbox "3,0", now the array1 looks like "1,0:2,0:3,0:4,0:3,0", but i need it as "1,0:2,0:4,0", where "3,0" is removed. – Abdul Jul 31 '13 at 12:22

4 Answers4

1

Similar question here: How to remove specifc value from array using jQuery

From the linked page (replacing their values with yours):

var array1 =[1,2,3,6,3,5,2,5,2,4,3];
var array2 =[1,2,3] 
var removeItem = array2[jQuery.inArray(3,array2)]; 

//jQuery.inArray returns the index where the value (3) first appears in the array (array2)

array1 = jQuery.grep(array1, function(value) {
    return value != removeItem;
});
console.log(array1); //[1,2,6,5,2,5,2,4]
Community
  • 1
  • 1
c.anna
  • 381
  • 1
  • 9
1

Try this:

function checkValueInArray(x){
    if(array1.indexOf(x) != -1 && array2.indexOf(x) != -1){
        //both arrays have this value and there indexes are
        //array1.indexOf(x);
        //array2.indexOf(x);
    }
}

checkValueInArray("3");
Mr_Green
  • 36,985
  • 43
  • 143
  • 241
1

Try

var arrayA = [1,2,3,6,3,5,2,5,2,4,3];
var arrayB = [3,4,5];
var arrayC = []; 

$('.arrayA').text('ArrayA: ' + arrayA);
$('.arrayB').text('ArrayB: ' + arrayB);

$.each(arrayA, function(indexA,valueA) {
    $.each(arrayB, function(indexB, valueB){
        if(valueA == valueB)
        {
            alert(valueA);
            alert(valueB);            
            return false;             
        }
    });
});
toesslab
  • 4,800
  • 8
  • 39
  • 59
Nitin Chaurasia
  • 243
  • 1
  • 5
0

Try with index of

if(array1.indexOf("3") != -1){
   //exists
}
Suresh Atta
  • 114,879
  • 36
  • 179
  • 284