Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

119
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda