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

135
Views
convert comma separated array of element to integer value in javascript

i have got one array with string value which contain comma separator.

const array = [
  "65,755.47",
  0,
  null,
  0,
]

i want to remove the string from the value and other value remain constant in the array.

so the the output should be:--

const array = [
  65,755.47,
  0,
  null,
  0,
]

How can i achieve this?Thanks!!

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

0

You can do it with a flat map

const arr = ["65,755.47", 0, null, 0];

const res = arr.flatMap((el) => {
  if (typeof el === "string") {
    return el.split(",").map((n) => +n);
  }
  return el;
});

console.log(res);

about 4 years ago · Juan Pablo Isaza Report

0

you can do this

const data = [
  "65,755.47",
  0,
  null,
  0,
]


const result1 = data.map(v => typeof v === 'string'?v.split(',').map(Number): v)
const result2 = data.flatMap(v => typeof v === 'string'?v.split(',').map(Number): v)

console.log(result1)
console.log(result2)

about 4 years ago · Juan Pablo Isaza Report

0

Given the array

const array = ["65755.47", 0, null, 0];

You can run this

const converted = array.map((item) => item ? Number(item) : item)

Or alternatively, you have strings with comma

const array2 = ["65,755.47", 0, null, 0];

you should remove commas before converting the item

const converted2 = array2.map((item) =>
  typeof item === "string" ? parseFloat(item.replace(/,/g, "")) : item
);
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!