Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.4k views
in Technique[技术] by (71.8m points)

js如何提取出包含在网址(路由)中的键值

let path = "/packageA/pages/insurance-through/index?enterprisename=哈哈哈公司&type=2&id=3c5322eeaaf44d1e925358e5f0d2b0be&amount=29,999,990&url=undefined"

该怎么样能提取出

let obj = {
    enterprisename:'哈哈哈公司',
    type:'2',
    id:'3c5322eeaaf44d1e925358e5f0d2b0be',
    amount:'29,999,990',
    url:''
}

在这里先感谢大婶们的解答!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
let afterUrl =  window.location.search.substring(1);//(问号以后的字符串)

let paramsArray = afterUrl.split('&')
let obj = {};
for (let i = 0; i<paramsArray.length; i++) {
    let arr = paramsArray[i].split("=");
    let key = arr[0];
    let value = arr[1];
    obj[key] = value;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...