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

151
Views
replace url params with values of an object

I have a object like this:

let data = {
    url: "https://test.ir/apps/:type/:id/",
    params: {
        id: "com.farsitel.bazaar",
        type: "xyz",
    },
    query: {
        ref: "direct",
        l: "en",
    },
};

I want to replace :type and :id in url with equivalent key to each from params object. what is the best solution in javascript?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Solution based on matching keys from params to the value of a regular expression from key url, followed by an update of that key.

On input: https://test.ir/apps/:type/:id/

On output: https://test.ir/apps/xyz/com.farsitel.bazaar/

let data = {
    url: "https://test.ir/apps/:type/:id/",
    params: {
        id: "com.farsitel.bazaar",
        type: "xyz",
    },
    query: {
        ref: "direct",
        l: "en",
    },
};

let new_url = data.url.replace(/:(\w+)/g, (match, key) => data.params[key] || match);

data.url = new_url;

console.log(data);

about 4 years ago · Juan Pablo Isaza Report

0

Could you just use String.replace?

const data = {
   url: "https://test.ir/apps/:type/:id/",
   params: {
      id: "com.farsitel.bazaar",
      type: "xyz",
   },
   query: {
      ref: "direct",
      l: "en",
   },
}

const url = data.url.replace(":type", data.params.type).replace(":id", data.params.id);

console.log(url)

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!