Query-string encoding of a Javascript Object

前端 未结 30 2918
渐次进展
渐次进展 2020-11-22 00:23

Do you know a fast and simple way to encode a Javascript Object into a string that I can pass via a GET Request?

No jQuery, no

30条回答
  •  无人共我
    2020-11-22 01:01

    In ES7 you can write this in one line:

    const serialize = (obj) => (Object.entries(obj).map(i => [i[0], encodeURIComponent(i[1])].join('=')).join('&'))
    

提交回复
热议问题