Can you demonstrate how to convert a string date array into a JavaScript array?

I am facing a challenge where I need to convert a string representation of an array into a JavaScript array for the purpose of looping. The string in question is enclosed within single quotes.

Inside my JavaScript code, I have the following:

var length1 = $('.length').text();  //['2018-9-24', '2018-9-26', '2018-9-25']
console.log(length1.length) // 39 as output but I need it to be 3 

The objective is to loop through each date mentioned in the array.

Any assistance on this matter would be highly appreciated.

I attempted the following:

var myArray=JSON.parse(length1); // However, this approach did not work  

Answer №1

Switch out single quotation marks for double and then interpret it:

var text = "['2018-9-24', '2018-9-26', '2018-9-25']";

console.log(JSON.parse(text.replace(/'/g, '"')));

Answer №2

I approached this task from a different angle

let datesString = "['2018-9-24', '2018-9-26', '2018-9-25']";
let datesArray = (new Function("return [" + datesString + "];")());

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Leveraging the power of the vuejs plugin within the main.js script

My goal is to develop a plugin to manage the OAuth2 token data in my Vue.js application. I followed several tutorials available on the internet to create this plugin. var plugin = {} plugin.install = function (Vue, options) { var authStorage = { ...

Deep Dive into TypeScript String Literal Types

Trying to find a solution for implementing TSDocs with a string literal type declaration in TypeScript. For instance: type InputType = /** Comment should also appear for num1 */ 'num1' | /** Would like the TSDoc to be visible for num2 as well ...

Determine the active animation on an element using jQuery or JavaScript

Can you provide the code for the know_anim() function that can determine which animation is currently running on the '#div' element? Check out the jsFiddle link for reference:https://jsfiddle.net/himavicii/bL0nsjeL/ function moveLeft() ...

Pair of elements connecting in Vuejs

What is the best way to efficiently organize data and communication between two Vue.js components? Here's an example scenario: 1) I have a component item(v-for="item in items) a {{item.name}} 2) And then the second component card(v-for="item in it ...

Tips for preserving the state of the Material-UI AutoComplete during component re-renders?

Currently, I am utilizing the Material-UI v4 AutoComplete component along with the renderOption prop in order to display a checkbox item option. The issue arises when the onChange event occurs and updates a hook in the parent component causing a re-rende ...

Issue with Mobile Touch Screen Preventing Vertical Scrolling

Currently experiencing difficulties with a div element that is not allowing touch and vertical scroll on mobile devices. Although scrolling works fine with the mouse wheel or arrow keys, it does not respond to touch. Have tested this on various devices and ...

Inquiry on how to compare and eliminate elements in array a[] that are also present in array b[] without regard to order

I've encountered a challenging problem that I need help with. Below are two unordered arrays: int a1[] = { 5, 7, 14, 0, 6, 2, 9, 11, 3 }; int n = 9; int b[] = { 6, 4, 3, 10, 9, 15, 7 }; int m = 7; My goal is to compare these arrays and remove eleme ...

Is there a way to update a single element within an array without triggering a refresh of the entire array?

Is there a way to prevent the component from rerendering every time new data is input? I attempted to memoize each cell but I am still facing the same issue. Any suggestions on how to accomplish this? import React, { useState, memo } from "react&quo ...

Tips for utilizing the "get" method in React to submit a form

Is there a way to submit a form using the "get" method to an external URL in react? Similar to how it is done in HTML like in this example: https://www.example.com/form But instead, to a third party URL...? Can I achieve something like this? The goal is ...

Tips for incorporating conditional statements within return statements in functional components in React JS

I need to display the login page if the user is not logged in, otherwise show the forbidden 403 page. Since I'm using a functional component, I can't use render(). return forbidden === false ? ( <> <Container maxWidth="x ...

Unable to eliminate top margin in Bootstrap 3 affix feature

Struggling to locate the source of the margin-top when utilizing the affix plugin on my Navbar. You can view [my website here] for better understanding. Should I be implementing Javascript to solve this issue? My current skills in that area are not very ...

The issue with the NextJS Layout component is that it is failing to display the page content, with an error message indicating that objects cannot be used

I am embarking on my first project using Next JS and after watching several tutorial videos, I find the Layout component to be a perfect fit for my project! However, I am encountering an issue where the page content does not display. The Menu and Footer a ...

The ValidationMessageFor tag is failing to function on the view page

I am currently in the process of updating the styling from bootstrap 3 to bootstrap 5. The issue I am facing is that in the bootstrap 3 version, when I click the "save" button without filling any text boxes, the page displays a validation message like this ...

Troubleshooting problems with jQuery Chosen plugin post-AJAX request

I'm encountering an issue with the plugin called jquery-chosen not applying to a control that is reconstructed by an ajax call. Despite exploring other suggestions, I have yet to find a solution that works. The versions of jquery and jquery-chosen be ...

Is it necessary to integrate the Facebook JavaScript SDK even if I do not plan on utilizing its features?

I have created some simple applications to use as custom tabs on my Facebook page, consisting of hyperlink images. I am not utilizing any of the functionality provided by the Facebook JavaScript SDK in these instances. Do I really need to load the Faceboo ...

Is there a way to enable multiple selections in a particular section?

Currently, I am in the process of working on my step by step order and I want to express my gratitude for all the assistance I have received so far. Despite the help, I still have some unanswered questions! The steps in my project are categorized into dif ...

Tips for inserting a button under a div when clicked

I am working on a layout where I have several div cards aligned side by side using the display: inline-block property. What I want to achieve is that when I click on a card, a button is added below the respective div. To accomplish this, I tried using jqu ...

How can you find the maximum value in a JSON array using PHP?

I have implemented the following code snippet to retrieve the distance between locations. <?php function curl_request($sURL,$sQueryString=null) { $cURL=curl_init(); curl_setopt($cURL,CURLOPT_URL,$sURL.'?'.$sQueryString); ...

When using AngularJS and Laravel together, the CORS functionality may cause the POST request to halt after the preflight

Once upon a time, there was a bird who dreamt of joining the postal service but failed his preflight test... Using Laravel as a RESTful API and AngularJS/ionic for the app, everything was working smoothly until suddenly... it stopped. The withCredentials ...

Enhancing PHP function speed through pre-compilation with Ajax

I am curious about compiling server side functions in PHP with Ajax, specifically when multiple asynchronous calls are made to the same server side script. Let's consider a PHP script called "msg.php": <?php function msg(){ $list1 = "hello world ...