Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check whether multiple values exist within an Javascript array

So, I'm using Jquery and have two arrays both with multiple values and I want to check whether all the values in the first array exist in the second.

For instance, example 1...

Array A contains the following values

34, 78, 89

Array B contains the following values

78, 67, 34, 99, 56, 89

This would return true

...example 2:

Array A contains the following values

34, 78, 89

Array B contains the following values

78, 67, 99, 56, 89

This would return false

...example 3:

Array A contains the following values

34, 78, 89

Array B contains the following values

78, 89

This would return false

So far I have tried to solve this by:

  1. Extending Jquery with a custom 'compare' method to compare the two arrays. Problem is this only returns true when the arrays are identical and as you can see from example 1 I want it to return true even if they aren't identical but at least contain the value
  2. using Jquerys .inArray function, but this only checks for one value in an array, not multiple.

Any light that anyone could throw on this would be great.

like image 374
Betjamin Avatar asked Feb 09 '12 01:02

Betjamin


People also ask

How do you check if an element exists in an array JavaScript?

You can use the includes() method in JavaScript to check if an item exists in an array. You can also use it to check if a substring exists within a string. It returns true if the item is found in the array/string and false if the item doesn't exist.

How do you check if a value is within an array?

The simplest and fastest way to check if an item is present in an array is by using the Array. indexOf() method. This method searches the array for the given item and returns its index. If no item is found, it returns -1.

How do you check if there are duplicates in an array JavaScript?

To check if an array contains duplicates: Use the Array. some() method to iterate over the array. Check if the index of the first occurrence of the current value is NOT equal to the index of its last occurrence. If the condition is met, then the array contains duplicates.

How do you check if an array has all the same values JavaScript?

To check if all values in an array are equal:Use the Array. every() method to iterate over the array. Check if each array element is equal to the first one. The every method only returns true if the condition is met for all array elements.


1 Answers

Native JavaScript solution

var success = array_a.every(function(val) {     return array_b.indexOf(val) !== -1; }); 

You'll need compatibility patches for every and indexOf if you're supporting older browsers, including IE8.

  • Compatibility patch from MDN for .every().
  • Compatibility patch from MDN for .indexOf().

Full jQuery solution

var success = $.grep(array_a, function(v,i) {     return $.inArray(v, array_b) !== -1; }).length === array_a.length; 

Uses $.grep with $.inArray.


ES2015 Solution

The native solution above can be shortened using ES2015's arrow function syntax and its .includes() method:

let success = array_a.every((val) => array_b.includes(val)) 
like image 172
5 revs, 2 users 86%user1106925 Avatar answered Sep 16 '22 13:09

5 revs, 2 users 86%user1106925