Question: In the project, the data format passed from the backend is like this. How do I get the value in BYMONTH?
INTERVAL=8;BYMONTH=9;BYMONTHDAY=17
Thinking: A simple method I thought of is to parse it into JSON:
var str = "INTERVAL=8;BYMONTH=9;BYMONTHDAY=17";
var fiStr = '"' + str.replace(/=/g,'":"').replace(/;/g,'","');
var lastST = '{' + fiStr + '"}';
var Obj = JSON.parse(lastST);
console.log(Obj.BYMONTH)
Question:
How should I handle this data format?
怪我咯2017-06-30 09:58:44
var res = {};
str.split(';').map(function(v){
var i = v.split('=');
res[i[0]]=i[1];
});
console.log(res['BYMONTH']);
黄舟2017-06-30 09:58:44
function getUrlParam(sUrl, sKey) {
var result,Oparam = {};
sUrl.replace(/[\;]?(\w+)=(\w+)/g,function($0,$1,$2){
Oparam[$1]=$2;
});
sKey === void 0||sKey==='' ? result=Oparam : result=Oparam[sKey]||'';
return result;
}
getUrlParam("INTERVAL=8;BYMONTH=9;BYMONTHDAY=17","BYMONTH") //9
大家讲道理2017-06-30 09:58:44
What I’m more curious about is why the backend doesn’t directly return json format? It has to be processed on the front end.