Skip to content
Malexion edited this page Jan 13, 2017 · 3 revisions

__.index(obj, [key], [value])

  • obj Item to iterate over.
  • [key] [Optional] Function to find the hash key, should be unique. Defaults to (value, key) => key
  • [value] [Optional] Function to find the hash value. Defaults to value => value

Simple hashmap generator for fast array lookups via brackets [] instead of linear searches.

Examples

var array = [ 3, 5, 1, 0, 8 ];
var index = __.index(array, value => value, (value, key) => key);

// I want the array index of value 8 in my array
var idx = index[8];
console.log(idx);

array = [
   { id: 300, name: 'Will' },
   { id: 0, name: 'Frank' },
   { id: 5, name: 'Shana' }
];
index = __.index(array, person => person.id);

// I want the person with id of 0
var person = index[0];
console.log(person);
Clone this wiki locally