Skip to content
Subhajit Sahu edited this page Mar 17, 2020 · 23 revisions

Searches a value from left.

array.search(x, v, [fn]);
// x:  an array
// v:  search value
// fn: compare function (a, b)
// --> index of value, -1 if not found
const array = require('extra-array');

array.bsearch([1, 3, 5, 7], 5);
// 2                 ^ found

array.bsearch([1, 3, 5, 7], 4);
// -3 (~2)           ^ not found, closest

array.bsearch([4, 4, 4, 4], 4);
// 0           ^ leftmost

array.bsearch(['b', 'GB', 'KB', 'MB'], 'kB', (a, b) => {
  return a.toLowerCase().localeCompare(b.toLowerCase());
});
// 2                       ^ case insensitive

references

Clone this wiki locally