js对象转超链接

search对象转为超链接

var search = {
    staff_name:{"value":"jack","operator":"like","type":"both"},
    enable_flag:{"value":"1","operator":"=","type":""},
    month:{"value":"","operator":"=","type":""},
}



var a = Object.keys(search).reduce((t, i) => {
    return t + Object.keys(search[i]).map(j => encodeURI(`search[${i}][${j}]=${search[i][j]}`)).join('&')
}, '')

console.log(a)

完整写法:

/**
 * 拼接搜索参数下载文件
 * @param {object} params 
 * search: {
 *      staff_name:{"value":"","operator":"like","type":"both"},
 *      enable_flag:{"value":"1","operator":"=","type":""},
 *      month:{"value":"","operator":"=","type":""}
 *  },
 * params {
 *  limit:1,
 *  search:search
 * }
 * 
 * 
 * @param {func} callback 
 */
export function openLink(baseHref,params) {
    let serachStr = ""
    if(params.hasOwnProperty("search")){
        let search = params.search;
        serachStr = Object.keys(search).reduce((t, i) => {
            return t + Object.keys(search[i]).map(j => encodeURI(`search[${i}][${j}]=${search[i][j]}`)).join('&')
        }, '')
        delete(params.search)
    }
    let simpleQuery = [];
    for (let key in params) {
        if (params.hasOwnProperty(key)) {
            console.log(key)
            let val = params[key];
            // if (val instanceof String || val instanceof Number || val instanceof Boolean){
                simpleQuery.push(key+`=`+val)
            // }
        }
    }
    let simpleQueryStr = simpleQuery.join("&")
    if (serachStr==""){
        window.open(base_url + baseHref + "?"+simpleQueryStr,"_blank")
    }else{
        window.open(base_url + baseHref + "?" + simpleQueryStr + "&" + serachStr, "_blank")
    }
}

 

posted @ 2020-02-12 12:36  许伟强  阅读(103)  评论(0编辑  收藏