5

Is there a function in Javascript / Jquery which will make hashtag from a string ? I'm looking for the answer for few minutes and i can't find it :/ Maybe there is some other way to make it ? I have Symfony 2.4 application.

I have my form data serialized to string, for example:

"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"

And i want to make hashtag from that, something like i Don't know '462423dfdaak542634'. I need later to compare hashtags to see if the form was changed or not.

kix
  • 3,290
  • 27
  • 39
Michal Olszowski
  • 795
  • 1
  • 8
  • 25

1 Answers1

14

Here, you can use this simple hashing function:

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

Abdul Jabbar
  • 2,573
  • 5
  • 23
  • 43
  • 1
    Welcome.. oh and btw.. hashtag referes to "#" so i was first thinking you want your strings converted to #String #anotherString.. haha – Abdul Jabbar Sep 26 '14 at 10:51
  • I know, My mistake. I later thought that there are hashtags on facebook and it wasn't luckiest word for what I need :P And now I know why I couldn't find an answer for this in google xD – Michal Olszowski Sep 26 '14 at 10:53
  • 1
    Works well, but i and char variables are not defined, and that's really dangerous. – user1702401 Jan 25 '17 at 17:42
  • 3
    Duplicate at least try making your own code OMG: https://stackoverflow.com/questions/7616461/generate-a-hash-from-string-in-javascript – T04435 Mar 16 '20 at 18:53