SOURCE

/* 
作者:飞行员ބ
链接:https://www.nowcoder.com/discuss/858847?type=post&order=create&pos=&page=1&ncTraceId=&channel=-1&source_id=search_post_nctrack&subType=2&gio_id=4B11D26016AC8D56CAC57B6E316F3069-1646894144711
来源:牛客网

function parseUrl(url) {
    // 代码实现这个函数
}
console.log(parseUrl("https://a.b.come?aaa=123&bbb=hhah&ccc=456"));
// 希望返回如下
{
    aaa: "123",
    bbb: "hhah",
    ccc: "456"
}
console.log(parseUrl("https://a.b.come?aaa=123&bbb=hhah&ccc=456&ccc=789"))
// 希望返回如下
{
    aaa: "123",
    bbb: "hhah",
    ccc: ["456", "789"]   
}
*/

function parseUrl(url) {
    const ret = {}
    let temp = url.split('?')[1].split('&')
    temp.forEach((arg) => {
        const [key, value] = arg.split('=')
        if (!ret.hasOwnProperty(key)) {
            ret[key] = value
        } else {
            if (Array.isArray(ret[key])) {
                ret[key].push(value)
            } else {
                ret[key] = [ret[key], value]
            }
        }
    })
    return ret
}

console.log(parseUrl("https://a.b.come?aaa=123&bbb=hhah&ccc=456&ccc=123&&ccc=abc"))
console 命令行工具 X clear

                    
>
console