url参数序列化

4年以前  |  阅读数:586 次  |  编程语言:JavaScript 
//将对象序列化成url参数传递
function stringifyUrl(search = {}) {
    return Object.entries(search).reduce(
        (t, v) => `${t}${v[0]}=${encodeURIComponent(v[1])}&`,
        Object.keys(search).length ? "?" : ""
    ).replace(/&$/, "");
}

console.log(stringifyUrl({
    age: 27,
    name: "YZW"
})); // "?age=27&name=YZW"

Copyright© 2013-2020

All Rights Reserved 京ICP备2023019179号-8