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") } }