用例
用例是基于提供的字符串或函数将对象数组转换为哈希映射,以评估和使用hashmap中的键作为对象本身的值.使用它的常见情况是将对象数组转换为对象的哈希映射.
码
以下是javascript中的小片段,用于将对象数组转换为哈希映射,由对象的属性值索引.您可以提供一个动态(运行时)评估哈希映射键的函数.希望这有助于将来的任何人.
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){ …
Run Code Online (Sandbox Code Playgroud) 我想构建像rapportive.com这样的chrome扩展.我是Chrome扩展程序和Gmail内容脚本的新手.任何人都可以建议如何解决这个问题?
目前我正在阅读Google的小工具文档.