4

I have five arrays that can contain the same value across them.

var arr1 = ['1','2','3','4'];
var arr2 = ['1','3']
var arr3 = ['1','2','3']
var arr4 = ['1','2','3','4','5'];
var arr5 = ['1','3','4']

How can I find all of the numbers that are in all of the arrays using jQuery?

The numbers 1 and 3 are in all of the arrays?

3
  • Are your arrays sorted? Commented Sep 23, 2015 at 18:09
  • You mean like array intersection? Commented Sep 23, 2015 at 18:11
  • @Ankit The arrays are not sorted Commented Sep 23, 2015 at 18:32

4 Answers 4

3

Check this out

Filtering array by duplicate elements

JsFiddle http://jsfiddle.net/nWjcp/87/

var arr1 = ['1','2','3','4'];
var arr2 = ['1','3']
var arr3 = ['1','2','3']
var arr4 = ['1','2','3','4','5'];
var arr5 = ['1','3','4']

var arrays = [
   arr1,arr2,arr3,arr4,arr5
    ];


var result = arrays.shift().filter(function(v) { // Filtering
    return arrays.every(function(a) {       // Seek duplicate
        return a.indexOf(v) !== -1;    
    });
});




   alert( JSON.stringify(result,null,4) ); // ['1','3']
Sign up to request clarification or add additional context in comments.

3 Comments

This is the better way to go.
Why the null and 4 ? To make things harder to understand ? does it contribute something to the answer ? not to mention that every+filter+indexOf are all need polyfills
@RoyiNamir Only if you need to support IE8, in which case you need a polyfill for pretty much everything.
2

First off, let's make a more manageable dataset:

var arrays = [
    ['1', '2', '3', '4'],
    ['1', '3'],
    ['1', '2', '3'],
    ['1', '2', '3', '4', '5'],
    ['1', '3', '4']
];

Then we'll want to get all the unique elements:

var elements = [].concat.apply([], arrays).filter(function(value, index, self) {
    return self.indexOf(value) === index;
}); //["1", "2", "3", "4", "5"]

And then finally, check this set against each of our arrays, keeping only the elements that are present in all of them:

var out = elements.filter(function(item) {
    return arrays.reduce(function(present, array) {
        present = present && (array.indexOf(item) !== -1);
        return present;
    }, true);
}); //["1", "3"]

Comments

1

I don't like the usage of methods without crossbrowser support :

Here is a simple solution without "extra" non-jquery-included functions (every , inArray,filter(array)):

var arr1 = ['1','2','3','4'];
var arr2 = ['1','3']
var arr3 = ['1','2','3']
var arr4 = ['1','2','3','4','5'];
var arr5 = ['1','3','4']

var arrs = [arr1,arr2,arr3,arr4,arr5];

var obj={};

$.each(arrs,function (i,arr){
     $.each(arr,function (j,n){
      obj[n]=(+obj[n] || 0) + 1;
     });
});


for (item in obj) 
{  
     if(obj.hasOwnProperty(item ) && obj[item]==arrs.length) console.log(item) //1,3
}

Comments

0

Find a long solution here. At first, let us find the array with least items so that search can be optimized:

var arr1 = ['1','2','3','4'];
var arr2 = ['1','3', '8'];
var arr3 = ['1','2','3'];
var arr4 = ['1','2','3','4','5'];
var arr5 = ['1','3','4'];

var arrayOfArray = [arr1, arr2, arr3, arr4, arr5];
var arrayWithMin = null;
var outputArray = [];
$.each(arrayOfArray, function(index, arrayItem) {
    arrayWithMin = (arrayWithMin == null) ? arrayItem : (arrayWithMin.length > arrayItem.length ? arrayItem : arrayWithMin);
});

$.each(arrayWithMin, function(i, searchItem) {
    if($.inArray(searchItem, arr1) > -1 
       && $.inArray(searchItem, arr2) > -1 
       && $.inArray(searchItem, arr3) > -1 
       && $.inArray(searchItem, arr4) > -1
       && $.inArray(searchItem, arr5) > -1) {
        outputArray.push(searchItem);
    }
});

document.write(outputArray.join(', '));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

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.