Repository URL to install this package:
|
Version:
1.2.8 ▾
|
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = exports.arrayToObject = void 0;
/**
* @SECURITY @BUGS @PERFORMANCE @DEPRECIATE
*
* ....spreading values like this...
* - no type safety - optimistic
* - putting the item[keyField]
* - may be a number,
* - or a string,
* - or already exist,
* - not even be ==
*
* @example
* /// hard to know the expectation
* var objs = [{'1': 1, '2': 2, '3': {three: true}}]
* arrayToObject(objs, '1')
* //=> {'1': objs}
*
* @example
* var objs = [1, 2, '2', Number(2)]
*
* arrayToObject(objs, objs)
* //=> {undefined: 2}
*
* /// and same result even WITH second arg
* arrayToObject(objs, objs)
*
* //=> undefined was default?
* //=> false positives?
* //=> what happened to my 3 2s?
*
* @example
*
* /// how do you debug where this went wrong?
* var objs = [1, 2, '2', Number(2)]
* arrayToObject([objs], '2.2')
* //=> {undefined: [objs]}
*
*/
const arrayToObject = (arr, keyField) => {
return Object.assign({}, ...arr.map(item => ({
[item[keyField]]: item
})));
};
exports.arrayToObject = arrayToObject;
var _default = arrayToObject;
exports.default = _default;