Plain Javascript Equivalent of jQuery.param()

后端 未结 7 1260
小蘑菇
小蘑菇 2021-02-07 11:04

jQuery.param() takes an array of key-value pairs, and turns it into a string you can use as a query string in HTML requests. For example,

a = {
      userid:1,
         


        
相关标签:
7条回答
  • 2021-02-07 11:51
    ES6 gives us some nice primitives:
    
        // Function that parses an object of string key/value params to build up
        // a string of url params
        // requires an object with no nested values
        export function parseUrlParams(urlParams) {
            const joinByEquals = (pair) => pair.join('=')
            const params = Object.entries(urlParams).map(joinByEquals).join('&')
            if (params) {
                return `?${params}`
            } else {
            return ''
            }
        }
    

    See it in action here: https://www.webpackbin.com/bins/-KnpOI6hb1AzTDpN3wS7

    0 讨论(0)
提交回复
热议问题