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

158
Views
How to convert "['2022-03-24', '2022-03-25']" to javascript array

I want to convert "['2022-03-24', '2022-03-25']" to javascript array data is from element.dataset

did const dateRange = JSON.stringify("['2022-03-24', '2022-03-25']") JSON.parse(dateRange) returns ['2022-03-24', '2022-03-25'] which is a string and not a javaScript array.

I want to populate date range with flatpickr with object from my database so I'm passing the date html element via data attribute <input type="date" class="date-range" data-date="{{date_range}}" />

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

0

Try that, it would leave you with array of date strings, try to parse them to dates now (if you need).

someString.replace(/[',\]\[]/g, '').split(' ')
about 4 years ago · Juan Pablo Isaza Report

0

You can use the Function() object to create an instance of a function that will return an array. The second line below creates an anonymous function which is then immediately invoked via ().

const arrString = "['2022-03-24', '2022-03-25']";
const arr = (new Function(`return ${arrString}`))();
console.log( arr );

DEMO

const arrString = "['2022-03-24', '2022-03-25']";
const arr = (new Function(`return ${arrString}`))();
console.log( arr );

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!