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

261
Views
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 answers
Answer question

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 Report

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 Report

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 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!