UNPKG

bbo

Version:

bbo is a utility library of zero dependencies for javascript.

32 lines (24 loc) 607 B
import './get_tag.js'; import isArray from './is_array.js'; import isString from './is_string.js'; /** * return an object from an array, keyed by the value at the given id */ function indexBy(arr, key) { if (!isArray(arr)) { throw new Error('expected an array for first argument'); } if (!isString(key)) { throw new Error('expected a string for second argument'); } var result = {}; var len = arr.length; for (var i = 0; i < len; i++) { var index = arr[i] && arr[i][key]; if (index) { result[index] = arr[i]; } } return result; } export default indexBy;