Reputation: 1949
I want to compare one array with another:
array1 = ['billy', 'bob', 'paul'];
array2 = ['billy', 'michael', 'bob'];
I want to detect whether array2 contains a name that isn't found in array1.
array2 can be longer or shorter than array1, in other words, array2 might be missing names or have more names than array1, but array2 cannot have a DIFFERENT name as compared to array1.
So far, I can detect whether array2 is longer than array 1. If it is, it is obviously adding names and is therefore not valid:
if (array1.length < array2.length) {
console.log('no');
}
but I this isn't as precise as it needs to be (if both arrays have an equal number of values, it returns true even if the individual vales don't correlate).
see the following for example scenarios:
array1 = ['billy', 'bob', 'paul'];
array2 = ['billy', 'b', 'paul']; //should not be valid
array1 = ['billy', 'b', 'paul'];
array2 = ['billy', 'bob', 'paul']; //should not be valid
array1 = ['billy', 'bob', 'paul'];
array2 = ['billy', 'michael', 'paul']; //should not be valid
array1 = ['billy', 'bob', 'paul'];
array2 = ['billy', 'bob', 'paul', 'michael']; //should not be valid
array1 = ['billy', 'bob', 'paul'];
array2 = ['billy', 'bob']; //this is valid
array1 = ['billy', 'bob', 'paul'];
array2 = ['billy']; //this is valid
array1 = ['bob', 'bob', 'billy', 'paul'];
array2 = ['paul', 'bob', 'bob', 'bob']; //this IS NOT valid
array1 = ['bob', 'bob', 'billy', 'paul'];
array2 = ['paul', 'bob', 'bob']; //this is valid
I'm assuming I should be using .every() but I am unsure as to how to implement it when comparing two arrays as all the examples I find test values of one array against a single value.
update: Array 2 cannot have more instances of a specific name than array 1, but it can have fewer.
Upvotes: 0
Views: 102
Reputation: 3512
If you want a simple and easy to understand solution, you don't need to use every. Just use the code below.
The code below uses a .forEach()
loop to loop through array2
and, uses another .forEach()
loop to find out if the value in array2
is also in array1
.
Note: There are many more efficient methods, but this method is easier to understand if you're new to programming and want to understand how this is done.
Edit: As you suggested in the comments, I fixed my code so each value in array2
has only one counterpart in array1
. Not every time it matches the value from array2
and array1
, it removes the value from array1
.
var array1 = ['bob', 'bob', 'billy', 'paul'];
var array2 = ['bob', 'bob', 'bob', 'billy'];
var contains = false;
var output = true;
array2.forEach(e2 => {
contains = false;
array1.forEach(e1 => {
if (e2 == e1) {
contains = true;
array1.splice(array1.indexOf(e1), 1);
}
})
if (!contains) {
output = false;
}
});
console.log(output);
Upvotes: 1
Reputation: 33726
This approach uses the function some
to stop when at least one name is not in array1
and the sum of the names is not the same between the arrays.
let isValid = (arr, arr2) => {
let sum = (array, n) => array.reduce((a, an) => a + (an === n), 0);
return !arr2.some(n => {
let sum2 = sum(arr2, n);
return !arr.some(an => an === n && sum(arr, an) === sum2);
});
};
console.log(isValid(['billy', 'bob', 'paul'], ['billy', 'b', 'paul'])); //should not be valid
console.log(isValid(['billy', 'b', 'paul'], ['billy', 'bob', 'paul'])); //should not be valid
console.log(isValid(['billy', 'bob', 'paul'],['billy', 'michael', 'paul'])); //should not be valid
console.log(isValid(['billy', 'bob', 'paul'], ['billy', 'bob', 'paul', 'michael'])); //should not be valid
console.log(isValid(['billy', 'bob', 'paul'], ['billy', 'bob'])); //this is valid
console.log(isValid(['billy', 'bob', 'paul'], ['billy'])); //this is valid
console.log(isValid(['bob', 'bob', 'billy', 'paul'], ['paul', 'bob', 'bob', 'bob'])); //this is NOT valid
console.log(isValid(['bob', 'bob', 'billy', 'paul'], ['paul', 'bob', 'bob'])); //this is valid
.as-console-wrapper { max-height: 100% !important; top: 0; }
Upvotes: 3
Reputation: 44125
You can use every
to check if every object in array2
is in array1
:
var array1 = ['billy', 'bob', 'paul'];
var array2 = ['billy', 'michael', 'bob'];
var allFound = array2.every(e => array1
.includes(e));
console.log(allFound); //Should return false because 'michael' is not in array1
This also works the other way:
var array1 = ['billy', 'bob', 'paul'];
var array2 = ['billy', 'michael', 'bob'];
var allFound = array1.every(e => array2.includes(e));
console.log(allFound); //Should return false because 'paul' is not in array2
As suggested in the comments, you can also make array1
(the one which you want to check against - see first example) a Set
, which is similar to an array but contains only unique values:
var array1 = ['billy', 'bob', 'paul'];
var array2 = ['billy', 'michael', 'bob'];
var array1Unique = new Set(array1);
var allFound = array2.every(e => array1Unique.has(e));
console.log(allFound); //Should return false because 'michael' is not in array1
Upvotes: 2
Reputation: 822
You can do the task with a simple for loop and .includes()
Example below:
var array1 = ['billy', 'michael', 'paul'];
var array2 = ['billy', 'michael', 'bob'];
var check = true;
for(var i = 0; i < array2.length; i++){
if(!array1.includes(array2[i])){
check = false;
break;
}
}
console.log(check);
Upvotes: 0