url中获取参数对象
function urlParamsToObject(url) {
//获取字符串 ? 后面的部分
const queryString = url.split('?')[1];
console.log(queryString);//a=2&b=3
const paramsArr = queryString.split('&');
console.log(paramsArr);//['a=2', 'b=3']
const paramObj = {};
paramsArr.forEach(param => {
const [key, value] = param.split('=');
paramObj[key] = decodeURIComponent(value);
});
return paramObj;
}
// 使用示例
const url = 'http/home/page?a=2&b=3';
const paramsObj = urlParamsToObject(url);
console.log(paramsObj);//{a: '2', b: '3'}
多条内容展示拼接符号问题
//多数据展示中间拼接符号,单数据展示不拼接符号
function showText(){
var obj={
apple1:'0',
apple2:'0',
apple3:'3',
apple4:'4',
apple5:'5',
}
var mapObj={
apple3:'小苹果',
apple4:'中苹果',
apple5:'大苹果',
}
let Text =''//展示话术
let objArr=[]
for(let key in obj){
if(obj[key]!=0){ //对源数据进行过滤,展示有数据的
objArr.push({[key]:obj[key],name:key.substring(key.length - 1)})
}
}
objArr.sort((a,b)=> b.name - a.name ) //先展示大的后展示小的
if(objArr.length>1){
objArr.forEach(item=>{
for(let itemkey in item){
if(itemkey!='name'){
Text += (mapObj[itemkey] + obj[itemkey] + '个、')
}
}
})
Text=Text.slice(0,-1)
}else if(objArr.length==1){
objArr.forEach(item=>{
for(let itemkey in item){
if(itemkey!='name'){
Text += (mapObj[itemkey] + obj[itemkey] + '个')
}
}
})
}
return Text
}
let write =showText()
console.log(write); //大苹果5个、中苹果4个、小苹果3个