javescript 字串生成 hash 值
阿新 • • 發佈:2021-01-14
技術標籤:jsjsjavascript前端
用來對比字串是否一致,一般是判斷已有文字是否被再次編輯。
function hashVal(string){
var hash = 0, i, chr;
if (string.length === 0) return hash;
for (i = 0; i < string.length; i++) {
chr = string.charCodeAt(i);
hash = ((hash << 5) - hash) + chr;
hash |= 0; // Convert to 32bit integer
}
return hash;
}
let string = '你好有緣人';
console.log(hashVal(string)); // -2000030674