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

176
Views
Find an alternative to split when processing a lot of data

I am mapping through the object in order to return an array of object. However I want to modify the "model-1111" from a string to an integer. This method works with a split, however I would like to find an alternative to split, because split will return an array, which is not the best when processing a lot of data.

const data = { "model-10389": 164703, "model-10388": 164704, "model-10387": 164705 };

const output = Object.entries(data).map(([key, id]) => ({
  modelId: +key.split("-").pop(),
  id
}));

console.log(output);

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

0

You can slice() or substring() the string from the indexOf() the '-'

const data = { "model-10389": 164703, "model-10388": 164704, "model-10387": 164705 };

const output = Object.entries(data).map(([key, id]) => ({
  modelId: +key.slice(key.indexOf("-") + 1),
  id
}));

console.log(output);

Or using RegExp to find the trailing digits (\d+$)

const data = { "model-10389": 164703, "model-10388": 164704, "model-10387": 164705 };

const output = Object.entries(data).map(([key, id]) => ({
  modelId: +key.match(/\d+$/),
  id
}));

console.log(output);

Or, as I believe Nina Scholz was hinting at in the comments, you could simply replace() the prefix with an empty string.

const data = { "model-10389": 164703, "model-10388": 164704, "model-10387": 164705 };

const output = Object.entries(data).map(([key, id]) => ({
  modelId: +key.replace('model-', ''),
  id
}));

console.log(output);

about 4 years ago · Juan Pablo Isaza Report

0

You could limit the split activities by supplying the second (optional) argument.

const data = {"model-10389-extra": 164703,  "model-10388": 164704,  "model-10387": 164705};

  const output = Object.entries(data).map(([key, id]) => ({
  modelId: +key.split("-",2)[1],
  id
  }));

 console.log(output);

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!