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

266
Vistas
JavaScript string manipulation: remove prefix, suffix and lowercase first (in single line without assignments), possible?

Hope that this will not be closed, I can't find any suitable questions.

  1. Prefix is a variable
  2. Suffix is always the word Value
  3. Resulting value should have the first letter lowercase

Code so far, missing the lowercase for the first letter:

// Input: { toastDurationValue: 3000, toastPositionValue: 'bottom' }
// Wanted: { duration: 3000, position: 'bottom' }

const options = Object.keys(element.dataset)
  .filter(k => k.startsWith(prefix))
  .reduce((prev, curr) => ({
    ...prev,
    [curr.slice(prefix.length).replace(/(Value)$/, '')]: element.dataset[curr]
  }), {});
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Just add

.toLowerCase()

after

.replace(/(Value)$/, '')

Here:

// Input: { toastDurationValue: 3000, toastPositionValue: 'bottom' }
const options = Object.keys(element.dataset)
  .filter(k => k.startsWith(prefix))
  .reduce((prev, curr) => ({
    ...prev,
    [curr.slice(prefix.length).replace(/(Value)$/, '').toLowerCase()]: element.dataset[curr]
  }), {});
console.log(options);

Output:

{ duration: 3000, position: 'bottom' }
about 4 years ago · Juan Pablo Isaza Denunciar

0

May below snippet can contribute to your solution.

const obj = { toastDurationValue: 3000, toastPositionValue: 'bottom' }
const prefix = 'toast';

Object.keys(obj).forEach(key => {
    obj[key] = obj[key.substring(0, key.lastIndexOf('Value')).replace(prefix, '').toLowerCase()] = obj[key];
    delete obj[key];
})

console.log(obj);

about 4 years ago · Juan Pablo Isaza Denunciar

0

This can be done in one statement, in an elaborate (difficult to read) way using slice(), replace() and toUpperCase(). The original string stays as it is:

let str = 'hellorandomWORDValue';
const prefix = 'hello';

const str2 = str.replace(prefix, "").slice(0,-5)[0].toUpperCase() + str.replace(prefix, "").slice(0,-5).slice(1)

console.log(str);
console.log(str2);

slice(0,5) -> removes the last 5 letter, so basically removes Value suffix.

replace(prefix,"") -> replaces first instances of the variable string prefix. So basically removes the prefix.

[0].toUpperCase() -> takes out the first character and makes it uppercase.

.slice(1) -> takes out rest of the string (from 1st index). Basically, rest of the string after first character.

const options = Object.keys(element.dataset)
  .filter(k => k.startsWith(prefix))
  .reduce((prev, curr) => ({
    ...prev,
    [curr.replace(prefix, "").slice(0,-5)[0].toUpperCase() + curr.replace(prefix, "").slice(0,-5).slice(1)]: element.dataset[curr]
  }), {});

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