Change a JSON array with dates into a date type array in JavaScript

There is an array available:

var BigWordsDates = JSON.parse('<?php echo addslashes($Array_OfDates_to_json) ?>');

When inspected in FireBug DOM, it appears as:

BigWordsDates   Object { #Tahrir=[36], #Egypt=[24], #Morsy=[16], more...}   
#AdminCourt ["2012-10-02","2012-10-02","2012-10-09", 2 more...]

The goal is to convert the array to a date format like this: 2012-FEB-06. Any guidance on how to accomplish this conversion to a CSV file would be greatly appreciated.

Answer №1

First, create a JSON object to store the months.

var month = {
  '1': 'JAN',
  '2': 'FEB',

  etc.
}

Next, parse the JSON data.

var output = [];

for(var k in BigWordsDates['#AdminCourt']) {
    var obj = BigWordsDates['#AdminCourt'][k]; // for example, '"2012-10-02"'
    var array = obj.split('-'); // array['2012', '10', '02']

    var new_value = array[0] + '-' + month[array[1]] + '-' + array[2];

    // add the new element to the output array
    output.push(new_value);
}

Remember to use try-catch blocks to debug any errors in the code.

Note that this approach works best if the structure of your JSON data remains consistent.

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

When it comes to assigning a background to a div using jQuery and JSON

I have been experimenting with creating a database using only JSON and surprisingly, it worked once I added a "js/" in the URL. However, my current issue lies with CSS. Let me elaborate. Here is the JSON data: [ { "title":"Facebook", ...

Understanding JSON by breaking it down into distinct sections with headers and rows

I'm encountering an issue that should be straightforward. I am trying to parse the given JSON data into a table view with category names as section headers and corresponding details within each category section. The JSON data consists of multiple cate ...

Retrieving a variable value from an AJAX call for external use

Looking for a solution to pass the generated JSON response from an ASMX web service accessed via AJAX to an outside variable in another function. Below is the code snippet for reference: function setJsonSer() { var strWsUrl = &apo ...

Utilizing JavaScript Plugin to Fix Image Source URLs in Incorrect Directories

I'm struggling and really in need of some assistance. The issue I'm facing is with a plugin that has a JavaScript file containing an array of URLs pointing to a "texture" directory within the plugin for images. However, I keep encountering 404 e ...

PHP - JSON does not create any output

I have developed a code that displays all the appointments I have for the day. The calendar layout is already set up. However, when I try to run the program using Python, it doesn't function as intended. Here is my code: <?php mysql_connect(dele ...

The component briefly displays the previous state before updating in the Material-UI Alert component

Whenever there is an error from an API while a user is registering, an alert is displayed on the form page. To handle this, an Alert component was created: <Snackbar open={open} autoHideDuration={9000} onClose={() => { setOpen(f ...

Navigating with Next.js Router: Dynamic URLs and the power of the back button

Utilizing the Router from the package next/router allows for a dynamic URL and loading of different content on the page: Router.push('/contract', `/contract/${id}`); An issue arises where the back button does not function as expected after runni ...

Incorporating a static background image slideshow in ASP.NET - a step-by-step guide

I am currently working on my asp.net website and I would like to incorporate an image slideshow as the background of my homepage. This idea was inspired by a site I came across, . I have successfully implemented the slideshow, but now I am wondering if it ...

Toggle visibility between 2 distinct Angular components

In my application, I have a Parent component that contains two different child components: inquiryForm and inquiryResponse. In certain situations, I need to toggle the visibility of these components based on specific conditions: If a user clicks the subm ...

From javascript to utilizing ajax calls to interact with php scripts,

I am currently working on a page called edit.php where I need to pass a JavaScript variable to a modal window containing PHP in order to execute a query and retrieve data. Unfortunately, my experience with Ajax is limited and I haven't been able to fi ...

Displaying form after Ajax submission

I have implemented an AJAX code to submit my form, but I am facing an issue where the form disappears after submission. Here is my current code: <script> $('#reg-form').submit(function(e){ e.preventDefault(); // Prevent Default Submissi ...

"AngularJS directive mandating the use of the required attribute for internal control

I've encountered a challenge with this specific issue. We are using a directive called deeplink, which contains the following code: restrict: 'E', require: 'ngModel', scope: { smDropdown: '=smDeeplinkDropdown', s ...

Using regular expressions to validate input in Javascript

Seeking assistance to validate an input text using the pattern <some_string>:<some_string> in JS/JQuery. For instance: A110:B120 AB12C:B123 I understand this might seem overly simplistic, but any guidance would be greatly appreciated. ...

Is the first part of the URL in Express susceptible to injections when using two-level URLs?

I am currently utilizing Node.js and Express for my project. When dealing with a single-level URL like: /estonia All scripts and styles are loading correctly. However, when it comes to a two-level URL such as: /estonia/tallinn The scripts and styles i ...

Refreshing Javascript with AngularJS

I'm encountering an issue while starting my angular js application. On a html page, I have divs with icons and I want the background color to change on mouse over. This is working using jquery's $(document).ready(function(){ approach. The conten ...

Button activates SVG animation

Currently, I am working on an animation for a button using SVG. The animation utilizes the classes .is-loading and .is-success. However, when I click on the button, the animation does not execute as expected. I'm having trouble identifying the error w ...

Could not locate the provider: $stateProvider

It's puzzling to me why this code is not recognizing the $stateProvider. Uncaught Error: [$injector:modulerr] Failed to instantiate module app due to: Error: [$injector:unpr] Unknown provider: $stateProvider This is a simple example of a module: ( ...

React Native encountered an error: `undefined` is not an object

Encountering the commonly known error message "undefined is not an object" when attempting to call this.refs in the navigationOptions context. Let me clarify with some code: static navigationOptions = ({ navigation, screenProps }) => ({ heade ...

What is the proper way for AJAX to function in WordPress when there is no output function available?

I am looking to incorporate AJAX functionality into my WordPress site to make a call to a third-party API. The goal is to update the state of some checkboxes based on the response received. While I have experience with AJAX, my previous implementations in ...

Every attempt to connect with the webservice via an ajax call has ended in failure

I am encountering an issue with a webservice call that I am making using jQuery's AJAX method. Despite receiving JSON data from the webservice when accessed directly through the browser, my site always triggers the error function rather than the succe ...