推荐关注
› Meteor
› JSLint - a JavaScript code quality tool
› jsFiddle
› D3.js
› WebStorm
推荐书目
› JavaScript 权威指南第 5 版
› Closure: The Definitive Guide
hoythan

js 有什么原生的方法可以吧 object 转为 string key=value 形式吗?

  •  
  •   hoythan ·
    PRO
    · Dec 26, 2017 · 5954 views
    This topic created in 3235 days ago, the information mentioned may be changed or developed.

    例如对象 { test:1, test2:2 }

    转成字符串

    test=1&test2=2

    10 replies  •  2018-01-11 10:38:23 +08:00
    SoloCompany
    SoloCompany
    Dec 26, 2017❤️2
    1
    Object.entries(o).reduce((arr, [k, v]) => arr.concat(encodeURIComponent(k) + '=' + encodeURIComponent(v)), []).join('&')
    hoythan
    hoythan
    OP
    PRO
    Dec 26, 2017
    2
    我以为有原生的方法~,不过这条棒棒的!
    airyland
    airyland
    Dec 26, 2017❤️1
    3
    在 Node.js 里自带 querystring 模块

    const querystring = require('querystring')
    querystring.stringify(obj)
    hahasong
    hahasong
    Dec 27, 2017 via iPad❤️1
    5
    jq 的表单序列化也可以
    Nioty
    Nioty
    Dec 27, 2017 via Android❤️1
    6
    用 of 迭代后 join 一下就可以
    shyling
    shyling
    Dec 27, 2017❤️1
    7
    Object.keys(a).reduce((a,b) => a+=`${b}=${a[b]}`, '')
    pmispig
    pmispig
    Jan 10, 2018
    9
    这个是 urlencode 啊,python 里面,js 不知道有没
    d41d8cd98f
    d41d8cd98f
    Jan 11, 2018
    10
    new URLSearchParams({ test:1, test2:2 }).toString()

    只有较新的浏览器才支持: https://caniuse.com/#feat=urlsearchparams
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   3057 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 63ms · UTC 04:41 · PVG 12:41 · LAX 20:41 · JFK 23:41
    ♥ Do have faith in what you're doing.