Javascript: Check if an Array is a Subset of Another Array

There are many use cases where knowing if an array is a subset of another can be quite useful – and although usually immediately obvious to the human eye, it can be difficult to evaluate in code.

In the code below, arr2 is a subset of arr1, but arr3 i…


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Johnny Simpson

There are many use cases where knowing if an array is a subset of another can be quite useful - and although usually immediately obvious to the human eye, it can be difficult to evaluate in code.

In the code below, arr2 is a subset of arr1, but arr3 is not.

let arr1 = [ 'a', 'b', 'c' ];
let arr2 = [ 'b', 'c' ];
let arr3 = [ 'c', 'd' ];

If we want to find out if an array is a subset of another array, the best way to evaluate this is by using the array every method. This method iterates through each element in an array and performs a test on it. If every element in the array passes that test, then the overall every method will return true.

For each evaluation, we have a "parent" array, and a "subset" array - where we want to check if the "subset" array is fully contained within the "parent" array.

To evaluate if one array is a subset of another, we can run every on the each element on the "subset" array. Within the every function, we can test if the "parent" array contains each element in the "subset" array. If it does, then every will return true. Otherwise, it'll return false.

This can be accomplished easily with a function like the one shown below - checkSubset will return true should the subsetArray be fully contained within the parentArray:

let arr1 = [ 'a', 'b', 'c' ];
let arr2 = [ 'b', 'c' ];
let arr3 = [ 'c', 'd' ];

let checkSubset = (parentArray, subsetArray) => {
    return subsetArray.every((el) => {
        return parentArray.includes(el)
    })
}

checkSubset(arr1, arr2); // returns true
checkSubset(arr1, arr3); // returns false


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Johnny Simpson


Print Share Comment Cite Upload Translate Updates
APA

Johnny Simpson | Sciencx (2022-11-13T18:34:41+00:00) Javascript: Check if an Array is a Subset of Another Array. Retrieved from https://www.scien.cx/2022/11/13/javascript-check-if-an-array-is-a-subset-of-another-array/

MLA
" » Javascript: Check if an Array is a Subset of Another Array." Johnny Simpson | Sciencx - Sunday November 13, 2022, https://www.scien.cx/2022/11/13/javascript-check-if-an-array-is-a-subset-of-another-array/
HARVARD
Johnny Simpson | Sciencx Sunday November 13, 2022 » Javascript: Check if an Array is a Subset of Another Array., viewed ,<https://www.scien.cx/2022/11/13/javascript-check-if-an-array-is-a-subset-of-another-array/>
VANCOUVER
Johnny Simpson | Sciencx - » Javascript: Check if an Array is a Subset of Another Array. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/11/13/javascript-check-if-an-array-is-a-subset-of-another-array/
CHICAGO
" » Javascript: Check if an Array is a Subset of Another Array." Johnny Simpson | Sciencx - Accessed . https://www.scien.cx/2022/11/13/javascript-check-if-an-array-is-a-subset-of-another-array/
IEEE
" » Javascript: Check if an Array is a Subset of Another Array." Johnny Simpson | Sciencx [Online]. Available: https://www.scien.cx/2022/11/13/javascript-check-if-an-array-is-a-subset-of-another-array/. [Accessed: ]
rf:citation
» Javascript: Check if an Array is a Subset of Another Array | Johnny Simpson | Sciencx | https://www.scien.cx/2022/11/13/javascript-check-if-an-array-is-a-subset-of-another-array/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.