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

399
Views
How can my Number or ParseInt() function avoid a string that is not a number

I am trying to return [1,2,{},3,4] without using flat() My code so far

function steamrollArray(arr) {
  
  let myArr= arr 
  .join(" ")
  .replace(/[\s+\W+]/g," ")
  .replace(/\s+/g," ")
  .split(" ")
return myArr
.map((arrr) => parseInt(arrr,10))
}

console.log(steamrollArray([1, {}, [3, [[4]]]]))

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

0

Just use recursion. No need to worry about types when you do not change them.

function cleanUp(arr, out=[]) {
  arr.forEach(item => {
    if (Array.isArray(item)) {
        cleanUp(item, out);
    } else {
      out.push(item);
    } 
  });
  return out;
}

console.log(cleanUp([1, {}, [3, [[4]]]]))

about 4 years ago · Juan Pablo Isaza Report

0

You can check if the vlaue is NaN and default to zero

isNaN(arrr)?0:parseInt(arrr,10))

function steamrollArray(arr) {
  
  let myArr= arr 
  .join(" ")
  .replace(/[\s+\W+]/g," ")
  .replace(/\s+/g," ")
  .split(" ")
return myArr
.map((arrr) => isNaN(arrr)?0:parseInt(arrr,10))
}

console.log(steamrollArray([1, {}, [3, [[4]]]]))

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!