Subsets and supersets
Immutable.js lists have a couple of complimentary methods that are handy for checking whether one list is part of another. Using these methods, you can avoid setting up your own elaborate reducing mechanism.
List subsets
If you want to know that a list belongs to another list, you can use the isSubset()
method:
const myList = List.of( List.of(1, 2, 3), List.of(4, 5, 6), List.of(7, 8, 9) ); const isSubset = List.of(1, 4, 7) .isSubset(myList.flatten()); console.log('isSubset', isSubset); // -> isSubset true
The myList
collection is a list of lists. So once you flatten it, you can pass it to the isSubset()
method when it's called on the list: List.of(1, 4, 7)
. This returns true
because myList
contains each of these values.
List supersets
The other approach is to use the isSuperset()
method, which determines if the argument is a subset of the collection where we're calling the method:
const myList = List.of( List.of(1, 2, 3), List.of(4, 5, 6), List.of(7, 8, 9...