import checkArray from '../array/_checkArray'; /** * Receives an array of objects and extracts an object with the given key as index. The key * must be a property of the array items. If the property is not found on the item, the item * is omitted. * * @memberOf object * @param {Object[]} array * @param {string} key * @param {boolean} keep - should the extracted prop be kept in the result object * @returns {*} */ const fromArray = (array, key, keep = true) => { checkArray(array); if (!(typeof key === 'string')) { throw Error('key must be a string'); } return array.reduce((object, item) => { const { prop, ...rest } = item; if (!prop) { return object; } object[prop] = keep ? item : rest; return object; }, {}); }; export default fromArray;