Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

365
Views
Please anyone can explain this JavaScript code, how is work?

This is JavaScript 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 {};
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

There is almost zero React here. It is quite plain JavaScript

export default (query) => { // this is the only react here
    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 whole thing can be written

export default (query) => {
  let urlParams = new URLSearchParams(query);
  const result = {};
  for (const [key, value] of urlParams.entries()) { 
    result[key] = value;
  }
  return result;
}

Test:

const getParams = query => {
  let urlParams = new URLSearchParams(query);
  const result = {}
  for (const [key, value] of urlParams.entries()) { // each 'entry' is a [key, value] tupple
    result[key] = value;
  }
  return result;
}

console.log(getParams("?cid=619386618c57a571f4463d45&type=page"))

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!