How to find values that exist in all Arrays.
Given the following arrays we want to get only the values that are present in every array.
const animals = ['cat', 'dog', 'lemur', 'tiger', 'lynx', 'bear', 'alligator', 'lion', 'jaguar']
const felines = ['cat', 'tiger', 'lynx', 'lion', 'jaguar']
const petOfTheTigerKing = ['cat', 'tiger', 'bear', 'alligator']
Getting all unique values in a new array
Start by creating a function existsInAllArrays
that will take in a parameter that is an array of arrays.
function existsInAllArrays(arrays) {}
existsInAllArrays([animals, felines, petOfTheTigerKing])
Flatten the arrays
The first step is to take all our values in our nested arrays and get them into the same non-nested array using the flat
method.
function existsInAllArrays(arrays) {
const flattened = arrays.flat()
// ['cat', 'dog', 'lemur', 'tiger', 'lynx', 'bear', 'alligator', 'lion', 'jaguar', 'cat', 'tiger', 'lynx', 'lion', 'jaguar', 'cat', 'tiger', 'bear', 'alligator']
}
existsInAllArrays([animals, felines, petOfTheTigerKing])
Getting all unique values
After flattening our nested arrays, we can create a new Set
object in order to get rid of the duplicate values.
function existsInAllArrays(arrays) {
const flattened = arrays.flat()
// ['cat', 'dog', 'lemur', 'tiger', 'lynx', 'bear', 'alligator', 'lion', 'jaguar', 'cat', 'tiger', 'lynx', 'lion', 'jaguar', 'cat', 'tiger', 'bear', 'alligator']
const uniqueValuesSet = new Set(flattened)
// Set { 'cat', 'dog', 'lemur', 'tiger', 'lynx', 'bear', 'alligator', 'lion', 'jaguar' }
}
existsInAllArrays([animals, felines, petOfTheTigerKing])
Convert back to an Array
Lets convert our newly created Set
object back to an array using the Array.from
method.
function existsInAllArrays(arrays) {
const flattened = arrays.flat()
// ['cat', 'dog', 'lemur', 'tiger', 'lynx', 'bear', 'alligator', 'lion', 'jaguar', 'cat', 'tiger', 'lynx', 'lion', 'jaguar', 'cat', 'tiger', 'bear', 'alligator']
const uniqueValuesSet = new Set(flattened)
// Set { 'cat', 'dog', 'lemur', 'tiger', 'lynx', 'bear', 'alligator', 'lion', 'jaguar' }
const uniqueValuesArray = Array.from(uniqueValuesSet)
// [ 'cat', 'dog', 'lemur', 'tiger', 'lynx', 'bear', 'alligator', 'lion', 'jaguar' ]
}
existsInAllArrays([animals, felines, petOfTheTigerKing])
In one line.
function existsInAllArrays(arrays) {
const uniqueValues = Array.from(new Set(arrays.flat()))
// [ 'cat', 'dog', 'lemur', 'tiger', 'lynx', 'bear', 'alligator', 'lion', 'jaguar' ]
}
existsInAllArrays([animals, felines, petOfTheTigerKing])
Checking if values exist in every array
Every method that we’ve used so far has not mutated our original arrays
parameter. Therefor we can check our unchanged arrays
against our newly created uniqueValues
array.
filter, every, and includes
In order to get only the values that exist in every array, we’ll use three array methods, filter
, every
, and includes
.
The logic goes like this:
- We’ll filter over our
uniqueValues
array, which takes a callback function that is called for every value and has access to the current element being processed in the array. - In the callback function we’ll use the
every
method on our originalarrays
parameter which also takes a callback function that is called for every value and has access to the current element being processed in the array. - In that callback function we’ll use the
includes
method which takes a value to search the array in the current iteration of theevery
method callback for.
To get a better understanding, our uniqueValues
array has 9 strings. The filter
method will loop over each of these values, so that callback will be called 9 times. For every loop of the filter method our every
method will be called on each array in our original arrays
parameter, therefor it’s callback function will run 9 * 3 = 27
times. For every one the those callbacks includes
checks the current array for the current value from the filter
method.
function existsInAllArrays(arrays) {
const uniqueValues = Array.from(new Set(arrays.flat()))
// [ 'cat', 'dog', 'lemur', 'tiger', 'lynx', 'bear', 'alligator', 'lion', 'jaguar' ]
const existsInAllArrays = uniqueValues.filter((value) => arrays.every((array) => array.includes(value)))
// [ 'cat', 'tiger' ]
}
existsInAllArrays([animals, felines, petOfTheTigerKing])
Final solution
const animals = ['cat', 'dog', 'lemur', 'tiger', 'lynx', 'bear', 'alligator', 'lion', 'jaguar']
const felines = ['cat', 'tiger', 'lynx', 'lion', 'jaguar']
const petOfTheTigerKing = ['cat', 'tiger', 'bear', 'alligator']
function existsInAllArrays(arrays) {
const uniqueValues = Array.from(new Set(arrays.flat()))
const existsInAllArrays = uniqueValues.filter((value) => arrays.every((array) => array.includes(value)))
return existsInAllArrays
}
const animalsInAllCategories = existsInAllArrays([animals, felines, petOfTheTigerKing])
// [ 'cat', 'tiger' ]