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

113
Views
JS - add ParseInt value to array

Working with bad data from backend, I need to convert strings to integers.

I have JSON:

{
    "cars": [
        "36",
        "49",
        "73"
    ]
}

I have an attempt to convert the strings to integers.

let cars= res.data.products[0].cars;
                for (const key of Object.keys(cars)) {
                    setSelectedCars(selCars=> [...selCars, parseInt(cars, 0)]);
                }

This attempt does not work as I loop it wrong and end with single value being present multiple times in array while "" remains.

The desired outcome would thus be:

{
    "cars": [
        36,
        49,
        73
    ]
}
almost 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Just use map and convert each to a number.

const data = {
    "cars": [
        "36",
        "49",
        "73"
    ]
}

data.cars = data.cars.map(Number);

console.log(data);

almost 4 years ago · Santiago Trujillo 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!