This is React code and this works perfectly but the problem is I can't understand how this works. If anyone knows how this works please explain it to me.
This is the query = ?cid=619386618c57a571f4463d45&type=page
export default (query) => {
if(query){
const queryString = query.split("?")[1];
if(queryString.length > 0){
const params = queryString.split("&");
const paramObj = {};
params.forEach(param => {
const keyValue = param.split("=");
paramObj[keyValue[0]] = keyValue[1];
});
return paramObj;
}
}
return {};
}
There is almost zero react here. It is quite plain JavaScript
export default (query) => {
if(query){ // if there is a string
const queryString = query.split("?")[1]; // get the stuff after the ?
if(queryString.length > 0){ // if there was stuff after the ?
const params = queryString.split("&"); // split on & (gives an array)
const paramObj = {}; // create an object
params.forEach(param => { // for each part of the split array
const keyValue = param.split("="); // split on the =
paramObj[keyValue[0]] = keyValue[1]; // use the string before the = as key and after as value
});
return paramObj; // return the object
}
}
return {}; // otherwise return empty object
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.