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

88
Views
Convert a string of an array to an array js

Why cant i convert this arr

let stringarr = "[2022/07/12, 2022/08/09]"

to this arr

let arr = JSON.parse(stringarr) ---> error

Unexpected token / in JSON at position 5

about 4 years ago · Santiago Gelvez
3 answers
Answer question

0

It's not valid JSON, since the array elements aren't quoted.

If the array elements are all dates formatted like that, you could use a regular expression to extract them.

let stringarr = "[2022/07/12, 2022/08/09]"
let dates = stringarr.match(/\d{4}\/\d{2}\/\d{2}/g);
console.log(dates);

about 4 years ago · Santiago Gelvez Report

0

It's to much simple 😄.

As your input is a valid array in string format. So, remove [ ] brackets and split with comma (,). Then it automatically generates an array.

let stringarr = "[2022/07/12, 2022/08/09]";
let arr = stringarr.replace(/(\[|\])/g, '').split(',');

Output:

['2022/07/12', ' 2022/08/09']
about 4 years ago · Santiago Gelvez Report

0

what can i do then to convert it to an array

There are several ways to do that, if the format of the string stays like this. Here's an idea.

console.log(`[2022/07/12, 2022/08/09]`
  .slice(1, -1)
  .split(`, `));

Or edit to create a valid JSON string:

const dateArray = JSON.parse(
  `[2022/07/12, 2022/08/09]`
    .replace(/\[/, `["`)
    .replace(/\]/, `"]`)
    .replace(/, /g, `", "`));
    
console.log(dateArray);

Or indeed use the match method @Barmar supplied.

about 4 years ago · Santiago Gelvez 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!