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

373
Views
Por favor, alguien puede explicar este código JavaScript, ¿cómo funciona?

Este es código JavaScript y funciona perfectamente, pero el problema es que no puedo entender cómo funciona. Si alguien sabe como funciona esto por favor que me lo explique.

Esta es la consulta = ?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

Hay casi cero React aquí. Es bastante simple 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 }

Todo se puede escribir

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

Prueba:

 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!