将对象数组转换为哈希映射,由对象的属性值进行索引

用例

用例是将一个对象数组转换为一个哈希映射,该哈希映射是基于string或者函数提供的,用来评估和使用哈希值和键值作为对象本身。 使用这种方法的常见情况是将对象数组转换为对象的哈希映射。

以下是javascript中的一小段代码,将对象数组转换为哈希映射,由对象的属性值进行索引。 您可以提供一个函数来dynamic地评估哈希映射的关键(运行时间)。 希望这有助于未来的任何人。

function isFunction(func){ return Object.prototype.toString.call(func) === '[object Function]'; } /** * This function converts an array to hash map * @param {String | function} key describes the key to be evaluated in each object to use as key for hasmap * @returns Object * @Example * [{id:123, name:'naveen'}, {id:345, name:"kumar"}].toHashMap("id") Returns :- Object {123: Object, 345: Object} [{id:123, name:'naveen'}, {id:345, name:"kumar"}].toHashMap(function(obj){return obj.id+1}) Returns :- Object {124: Object, 346: Object} */ Array.prototype.toHashMap = function(key){ var _hashMap = {}, getKey = isFunction(key)?key: function(_obj){return _obj[key];}; this.forEach(function (obj){ _hashMap[getKey(obj)] = obj; }); return _hashMap; }; 

你可以在这里find要点: https : //gist.github.com/naveen-ithappu/c7cd5026f6002131c1fa

这对于Array.prototype.reduce是相当简单的:

 var arr = [ { key: 'foo', val: 'bar' }, { key: 'hello', val: 'world' } ]; var result = arr.reduce(function(map, obj) { map[obj.key] = obj.val; return map; }, {}); console.log(result); // { foo: 'bar', hello: 'world' } 

注意: Array.prototype.reduce()是IE9 +,所以如果你需要支持旧浏览器,你需要填充它。

使用ES6 Map ( 很好的支持 ),你可以试试这个:

 var arr = [ { key: 'foo', val: 'bar' }, { key: 'hello', val: 'world' } ]; var result = new Map(arr.map((i) => [i.key, i.val])); console.log(result); // Map {"foo" => "bar", "hello" => "world"} 

用lodash ,这可以使用keyBy完成:

 var arr = [ { key: 'foo', val: 'bar' }, { key: 'hello', val: 'world' } ]; var result = _.keyBy(arr, o => o.key); console.log(result); // Object {foo: Object, hello: Object} 

以下是我在javascript中创build的小片段,用于将对象数组转换为哈希映射,由对象的属性值进行索引。 您可以提供一个函数来dynamic地评估哈希映射的关键(运行时间)。

 function isFunction(func){ return Object.prototype.toString.call(func) === '[object Function]'; } /** * This function converts an array to hash map * @param {String | function} key describes the key to be evaluated in each object to use as key for hasmap * @returns Object * @Example * [{id:123, name:'naveen'}, {id:345, name:"kumar"}].toHashMap("id") Returns :- Object {123: Object, 345: Object} [{id:123, name:'naveen'}, {id:345, name:"kumar"}].toHashMap(function(obj){return obj.id+1}) Returns :- Object {124: Object, 346: Object} */ Array.prototype.toHashMap = function(key){ var _hashMap = {}, getKey = isFunction(key)?key: function(_obj){return _obj[key];}; this.forEach(function (obj){ _hashMap[getKey(obj)] = obj; }); return _hashMap; }; 

你可以在这里find要点: https : //gist.github.com/naveen-ithappu/c7cd5026f6002131c1fa

使用简单的Javascript

 var createMapFromList = function(objectList, property) { var objMap = {}; objectList.forEach(function(obj) { objMap[obj[property]] = obj; }); return objMap; }; // objectList - the array ; property - property as the key 

es2015版本:

 const myMap = new Map(objArray.map(obj => [ obj.key, obj.val ]));