碎片时间学编程「288]:将字符串哈希为数字

将输入字符串散列为整数。
使用String.prototype.split()和Array.prototype.reduce()方法创建输入字符串的散列,利用位移。
const sdbm = str => { let arr = str.split(''); return arr.reduce( (hashCode, currentVal) => (hashCode = currentVal.charCodeAt(0) + (hashCode << 6) + (hashCode << 16) - hashCode), 0 );};
示例:
sdbm('name'); // -3521204949
更多内容请访问我的网站:https://www.icoderoad.com

