You can use JavaScript's built in Array prototype methods i.e. find
, findIndex
, includes
, and some
.
const
existsIn = (sm, lg) => lg.some(e => sm.includes(e)),
firstMatch = (sm, lg) => lg.find(e => sm.includes(e)),
indexOfFirstMatch = (sm, lg) => lg.findIndex(e => sm.includes(e)),
larger = ['A19', 'A4', 'A1', 'A8'],
smaller = ['A1', 'A3', 'B7'];
console.log(existsIn(smaller, larger)); // true
console.log(firstMatch(smaller, larger)); // 2
console.log(indexOfFirstMatch(smaller, larger)); // A1
Alternatively, you could cache the smaller list, prior to checking the larger list. This alleviates the use of calling Array.prototype.includes
on the smaller list.
const
lookup = a => a.reduce((r, e, i) => ({ ...r, [e]: i }), {}),
existsIn = (sm, lg) => (lu => lg.some(e => lu[e] != null))(lookup(sm)),
firstMatch = (sm, lg) => (lu => lg.find(e => lu[e] != null))(lookup(sm)),
indexOfFirstMatch = (sm, lg) => (lu => lg.findIndex(e => lu[e] != null))(lookup(sm)),
larger = ['A19', 'A4', 'A1', 'A8'],
smaller = ['A1', 'A3', 'B7'];
console.log(existsIn(smaller, larger)); // true
console.log(firstMatch(smaller, larger)); // 2
console.log(indexOfFirstMatch(smaller, larger)); // A1
You could further modify this into a thunk/closure, but you must recache, if the smaller list if modified.
const
lookup = a => a.reduce((r, e, i) => ({ ...r, [e]: i }), {}),
finder = sm => (lu => () => ({
existsIn : lg => lg.some(e => lu[e] != null),
firstMatch : lg => lg.find(e => lu[e] != null),
indexOfFirstMatch : lg => lg.findIndex(e => lu[e] != null)
}))(lookup(sm))(),
larger = ['A19', 'A4', 'A1', 'A8'],
smaller = ['A1', 'A3', 'B7'],
find = finder(smaller);
console.log(find.existsIn(larger)); // true
console.log(find.firstMatch(larger)); // 2
console.log(find.indexOfFirstMatch(larger)); // A1