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

117
Views
Replace array values in string and make new string as per the values

I have one array which is range and on string which is test. Now I want to replace my range array values with {q1} and replace {w1} with it's values. I added Expected output for my scenario

let range = [25, 50, 100, 125];
let w1 = 2
let test = '{w1} + {q1}'

Expected output

let string1 = 2 + 25;
let string2 = 2 + 50;
let string3 = 2 + 100;
let string4 = 2 + 125;
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

const range = [25, 50, 100, 125];
const w1 = 2;
const result = range.map(x=> w1.toString() + " + " + x.toString());

const range = [25, 50, 100, 125];
  const w1 = 2;
  const result = range.map((x) => w1.toString() + " + " + x.toString());
  console.log("res", result);

about 4 years ago · Juan Pablo Isaza Report

0

You can simple achieve this using map and array destructuring assignment.

let range = [25, 50, 100, 125];
let w1 = 2;

const [string1, string2, string3, string4] = range.map(r => `${w1} + ${r}`);

console.log(string1);
console.log(string2);
console.log(string3);
console.log(string4);

about 4 years ago · Juan Pablo Isaza Report

0

You can replace string parts using String.prototype.replace() like this:

const range = [25, 50, 100, 125];
const w1 = 2;
range.map(value => '{w1} + {q1}'.replace('{w1}', w1).replace('{q1}', value));
// ['2 + 25', '2 + 50', '2 + 100', '2 + 125']

But it's a bad practise. Much better to use string interpolation via Template literals like this:

const range = [25, 50, 100, 125];
const w1 = 2;
range.map(value => `${w1} + ${value}`);
// ['2 + 25', '2 + 50', '2 + 100', '2 + 125']
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!