Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a JavaScript array contains two specific values only [duplicate]

Tags:

javascript

I need to check if an array includes two values and only them.

This is my ugly solution for now:

if (myArray.includes('foo') && myArray.includes('bar') && myArray.length === 2) {
    // Do something
}

Update:

I may have to check three or more values as well. Any elegant solution? By the way, I'm using Lodash in this project.

like image 606
isar Avatar asked Jul 30 '18 14:07

isar


People also ask

How do you check if an array of objects has duplicate values in JavaScript?

Using the indexOf() method In this method, what we do is that we compare the index of all the items of an array with the index of the first time that number occurs. If they don't match, that implies that the element is a duplicate.All such elements are returned in a separate array using the filter() method.

How do you find multiple duplicate elements in an array?

Duplicate elements can be found using two loops. The outer loop will iterate through the array from 0 to length of the array. The outer loop will select an element. The inner loop will be used to compare the selected element with the rest of the elements of the array.

How do you find duplicate numbers in an array if it contains multiple duplicates JavaScript?

Using the filter() and indexOf() Methods This is the shortest and the easiest way of finding duplicates in an array, where the filter() method traverses the array and filters items according to the defined condition and returns a new array, while the indexOf() gives the index of the passed item.

How do you find duplicate numbers in an array if it contains multiple duplicates?

Simple Approach: The idea is to use nested loop and for each element check if the element is present in the array more than once or not. If present, then store it in a Hash-map.


2 Answers

I may have to check three or more values as well.

You could present your elements as an array, so if you've more than two items to check, the condition will still short and the same, using .every() method like:

myArray.every(elem => [1,2].indexOf(elem) > -1)

var myArray = [2, 1];
var myItems = [1, 2];

if (myArray.length === myItems.length && myArray.every(elem => myItems.indexOf(elem) > -1)) {
  console.log('PASS');
}

UPDATE:

Since you're using lodash, you could simply use _.difference() like:

if (_.size(myArray) === _.size(myItems) && _.difference(myArray, myItems).length === 0) {
    // Do something
}
like image 89
Zakaria Acharki Avatar answered Oct 06 '22 11:10

Zakaria Acharki


That is the proper way which you have used. But to improve the comparison you can put myArray.length === 2 at the begining as if the length is not 2 it will fail immediately:

if (myArray.length === 2 && myArray.includes('foo') && myArray.includes('bar')) {
  // do something
}

If your want to compare more values then you can check that with the help of a custom function:

function checkIncluded(myArray, checkingArray){
  if(myArray.length !== checkingArray.length){
    return false;
  }
  var match = true;
  for(var i=0; i<checkingArray.length; i++){
    if(!myArray.includes(checkingArray[i])){
      match = false;
      break;
    }
  }
  return match;
}


var neededItems = ['foo', 'bar', 'pat', 'jack'];
var myArray = ['jack', 'bar', 'foo', 'pat']
var included = checkIncluded(myArray, neededItems);
if (included) {
  console.log('All items matched');
} else {
  console.log('All items do not matched');
}

neededItems = ['bar', 'pat', 'jack'];
myArray = ['jack', 'bar', 'foo', 'pat']
included = checkIncluded(myArray, neededItems);
if (included) {
  console.log('All items matched');
} else {
  console.log('All items do not matched');
}
like image 22
Ankit Agarwal Avatar answered Oct 06 '22 10:10

Ankit Agarwal