字符串到Javascript / Jquery中的唯一哈希

时间:2014-09-26 10:35:03

标签: javascript jquery

Javascript / Jquery中是否有一个函数可以从字符串中生成hashtag?我正在寻找几分钟的答案,我找不到它:/也许有其他方法可以做到这一点?我有Symfony 2.4应用程序。

我将表单数据序列化为字符串,例如:

"cloud_adm_dictionary_type%5Bitems%5D%5B0%5D%5BdictName%5D=Otwartabbb&cloud_adm_dictionary_type%5Bitems%5D%5B0%5D%5BdictValue1%5D=fa-comments-o+text-muted&cloud_adm_dictionary_type%5Bitems%5D%5B0%5D%5BdictActive%5D=1&cloud_adm_dictionary_type%5Bitems%5D%5B1%5D%5BdictName%5D=Wycena&cloud_adm_dictionary_type%5Bitems%5D%5B1%5D%5BdictValue1%5D=fa-comments-o+text-muted&cloud_adm_dictionary_type%5Bitems%5D%5B1%5D%5BdictActive%5D=1&cloud_adm_dictionary_type%5Bitems%5D%5B2%5D%5BdictName%5D=Negocjacje&cloud_adm_dictionary_type%5Bitems%5D%5B2%5D%5BdictValue1%5D=fa-comments-o+text-muted&cloud_adm_dictionary_type%5Bitems%5D%5B2%5D%5BisDefault%5D=1&cloud_adm_dictionary_type%5Bitems%5D%5B2%5D%5BdictActive%5D=1&cloud_adm_dictionary_type%5Bitems%5D%5B3%5D%5BdictName%5D=Wygrana&cloud_adm_dictionary_type%5Bitems%5D%5B3%5D%5BdictValue1%5D=fa-thumbs-o-up+text-primary&cloud_adm_dictionary_type%5Bitems%5D%5B3%5D%5BdictActive%5D=1&cloud_adm_dictionary_type%5Bitems%5D%5B4%5D%5BdictName%5D=Przegrana&cloud_adm_dictionary_type%5Bitems%5D%5B4%5D%5BdictValue1%5D=fa-thumbs-o-down+text-danger&cloud_adm_dictionary_type%5Bitems%5D%5B4%5D%5BdictActive%5D=1&cloud_adm_dictionary_type%5B_token%5D=3PBJr_pPjHhAIB95N7PUReP5asrXsGwCILAxZLyGTUg deal:738
cloud_adm_dictionary_type%5Bitems%5D%5B0%5D%5BdictName%5D=w%C5%82asnee&cloud_adm_dictionary_type%5Bitems%5D%5B0%5D%5BdictActive%5D=1&cloud_adm_dictionary_type%5Bitems%5D%5B1%5D%5BdictName%5D=proklienckii&cloud_adm_dictionary_type%5Bitems%5D%5B1%5D%5BdictActive%5D=1&cloud_adm_dictionary_type%5Bitems%5D%5B2%5D%5BdictName%5D=telemarketing&cloud_adm_dictionary_type%5Bitems%5D%5B2%5D%5BdictActive%5D=1&cloud_adm_dictionary_type%5Bitems%5D%5B3%5D%5BdictName%5D=mailing&cloud_adm_dictionary_type%5Bitems%5D%5B3%5D%5BdictActive%5D=1&cloud_adm_dictionary_type%5Bitems%5D%5B4%5D%5BdictName%5D=www&cloud_adm_dictionary_type%5Bitems%5D%5B4%5D%5BdictActive%5D=1&cloud_adm_dictionary_type%5Bitems%5D%5B5%5D%5BdictName%5D=partner&cloud_adm_dictionary_type%5Bitems%5D%5B5%5D%5BdictActive%5D=1&cloud_adm_dictionary_type%5Bitems%5D%5B6%5D%5BdictName%5D=nowe+zrodlo&cloud_adm_dictionary_type%5Bitems%5D%5B6%5D%5BdictActive%5D=1&cloud_adm_dictionary_type%5Bitems%5D%5B7%5D%5BdictName%5D=Aaa&cloud_adm_dictionary_type%5Bitems%5D%5B7%5D%5BdictActive%5D=1&cloud_adm_dictionary_type%5B_token%5D=3PBJr_pPjHhAIB95N7PUReP5asrXsGwCILAxZLyGTUg"

我想从那里制作标签,就像我不知道'462423dfdaak542634'。我稍后需要比较主题标签以查看表单是否已更改。

1 个答案:

答案 0 :(得分:7)

在这里,您可以使用这个简单的散列函数:

function hashCode (str){
    var hash = 0;
    if (str.length == 0) return hash;
    for (i = 0; i < str.length; i++) {
        char = str.charCodeAt(i);
        hash = ((hash<<5)-hash)+char;
        hash = hash & hash; // Convert to 32bit integer
    }
    return hash;
}

<强> See the DEMO here