UNPKG

locutus

Version:

Locutus other languages' standard libraries to JavaScript for fun and educational purposes

22 lines (21 loc) 733 B
export function match(value, table, nomatch = null) { // discuss at: https://locutus.io/r/match/ // original by: Kevin van Zonneveld (https://kvz.io) // note 1: Returns the first 1-based match position, similar to R match. // note 2: Returns nomatch when provided, otherwise null. // example 1: match('b', ['a', 'b', 'c']) // returns 1: 2 // example 2: match('z', ['a', 'b', 'c']) // returns 2: null // example 3: match(3, [1, 2, 3, 2], 0) // returns 3: 3 if (!Array.isArray(table)) { return nomatch; } for (let i = 0; i < table.length; i++) { if (Object.is(table[i], value)) { return i + 1; } } return nomatch; }