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.

3
  • 1
    Please demonstrate some attempt at solving the problem yourself. Commented Jul 31, 2013 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? Commented Jul 31, 2013 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. Commented Jul 31, 2013 at 12:22

4 Answers 4

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"); 
Sign up to request clarification or add additional context in comments.

Comments

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] 

Comments

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

Comments

0

Try with index of

if(array1.indexOf("3") != -1){ //exists } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.