Modify JSON date format to a shorter version using the first 2 letters of the month and the year

Looking to convert date values in a JSON array from "December 2016" format to "D16". Hoping to accomplish this using Regex, any assistance would be greatly appreciated.

[["November 2016","December 2016","January 2017","February 2017","March 2017"],["total",2,43,59,51,41
],["mammo",1,20,33,12,12],["face",1,20,16,30,27],["body",0,3,10,9,2],["photo",0,19,27,12,5],["scan",2
,24,32,39,36]]

Answer №1

let months = ["November 2016","December 2016","January 2017","February 2017","March 2017"];

months = months.map((month) => month[0] + month.split(' ')[1].slice(2, 4))

console.log(months)
//["N16", "D16", "J17", "F17", "M17"]

If you require more complex date manipulations, consider using a library like moment.js.

Answer №2

To accomplish this task, you can utilize a regular expression to identify the necessary text and update the elements of the initial date array. Give this method a try:

var data = [
  ["December 2016", "January 2017", "February 2017", "March 2017", "April 2017"],
  ["total", 5, 23, 36, 48, 54],
  ["mammo", 2, 20, 35, 14, 11],
  ["face", 3, 22, 19, 25, 29],
  ["body", 1, 8, 15, 18, 5],
  ["photo", 2, 16, 24, 13, 8],
  ["scan", 4, 30, 28, 37, 44]
];

data[0] = data[0].map(function(month) {
  return month.replace(/^(\w{1}).*(\d{2})$/gi, "$1$2");
})

console.log(data);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Answer №3

Seems like a solid solution

 ['December17'].map(word => word.substring(0,1).concat(word.slice(-2)))

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 the mouse is clicked, the character fails to reach the intended destination or moves in the wrong direction on the HTML canvas

UPDATE: RESOLVED I am currently working on a game where the character moves by right-clicking. The character is meant to walk slowly, not teleport, towards the destination set by right-clicking on the canvas. However, I have encountered an issue where the ...

Sending data via an AJAX POST request in the request parameter

I have a question regarding the method of making a POST request in my code: $.ajax({ url :"/clientCredentials.json", type: "POST", data: { "clientEmail": email, "clientName":clientName, "orgName":orgName, "l ...

Troubleshooting problems with jqplot pie charts

Currently, I am utilizing jqplot to create pie charts. Despite following the example code provided in the jqplot documentation, I am encountering errors such as "e.jqplot is undefined." Below is a snippet of my code: <script src="jquery-2.0.3.js"> ...

What is the best way to monitor the quantity of server-side requests being made while utilizing jQuery Validate's remote feature?

Currently, I am implementing jQuery Validate to handle form validation on my website. In this process, I'm utilizing an Ajax call to verify certain data and I find the need to keep track of the number of attempts or calls made to the server. For inst ...

How can I retrieve properties from a superclass in Typescript/Phaser?

Within my parent class, I have inherited from Phaser.GameObjects.Container. This parent class contains a property called InformationPanel which is of a custom class. The container also has multiple children of type Container. I am attempting to access the ...

How can we utilize dynatree.js lazy loading to eliminate null child nodes (i.e., every final node in a branch is null)?

I just started using dynatree and so far it's been great with lazy loading. However, I'm facing an issue where the last child is displaying as null for every branch. $("#tree").dynatree({ title: "Lazy loading sample", fx: { height: "togg ...

When using Lockdown.js with NPM, I encounter a blank file being returned

When using Lockdown.js for NPM, I encountered an issue where the command to generate the lockdown file resulted in an empty file. Here are the links for the dependencies: NPM lockdown git Here is a snippet from my package.json: { "name": "nw", "pri ...

View the selected radio buttons and checkboxes next to each other in real-time before finalizing and submitting

Within my form, individuals have the option to select from radio buttons and checkboxes. The challenge is that I need to display the chosen data on the page before they enter their email and submit! Since I cannot refresh the page, PHP won't work for ...

Error encountered: No geographic indices found for executing a geoNear operation with Mongoose

Initially, I had divided the schemas but later nested them inside my overall document. Despite adding indexes and removing coordinates from location, there seems to be an issue with the nested points. Upon running get Indexes, it shows that there is an i ...

Dealing with redirecting to the login page in Angular

I recently started working with Angular and I feel completely lost. My initial task involves making a simple Rest-GET request, but the destination is located behind an external login page. This results in my request being redirected to the external page a ...

Accessing properties in JavaScript using square brackets

When I input the following code into my Chrome console: var object = {0:0, 1:1} I am able to retrieve the values by calling object[0] and object[1]. Surprisingly, even when I use object["0"] and object["1"], I still get the same results. Then, if I redef ...

MUI is designed to only manage either onBlur or onKeyPress, but not both simultaneously

Currently, I am working on a project with TypeScript and Material-UI. My main goal is to handle both the onBlur event and the onEnter key press event for a TextField component. Here's the scenario: I have incorporated this text field into a menu. Whe ...

Checking the authenticity of trello board API responses with Python unittest

My current task involves writing a unittest that interacts with the Trello board API to validate the presence of a specific card. Initially, I attempted to use the /1/boards/[board_id]/lists endpoint to retrieve data in the following format: [{'card ...

Is it a common occurrence for AJAX applications utilizing POST requests to encounter issues in Internet Explorer?

After some investigation, I have come across a bug in Internet Explorer that is causing intermittent failures for users running my application. This bug exists within the HTTP stack of IE and impacts all applications utilizing POST requests from this brows ...

How can JavaScript be integrated with Django static URLs?

I have a Django application where I store static images on Digital Ocean Spaces. Displaying these static images in my template is simple:<img>{% static 'images/my_image.png' %}</img> When I inspect the HTML page after loading, I see ...

Executing a cURL request using Node.js

Looking for assistance in converting the request below: curl -F <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1a777f7e737b275a73777b7d7f34706a7d">[email protected]</a> <url> to an axios request if possible. ...

Unraveling JSON in Python

I'm facing a challenge with my Python script that needs to read a JSON file and trigger a routine on a PI once a certain condition is met. The issue lies in reading specific data from the JSON file, particularly under "StrikeData". The JSON structure ...

Using Angular 6's httpClient to securely post data with credentials

I am currently working with a piece of code that is responsible for posting data in order to create a new data record. This code resides within a service: Take a look at the snippet below: import { Injectable } from '@angular/core'; import { H ...

Issue with Sequelize Associate function not functioning correctly

I've been attempting to connect two tables in Sequelize, but I keep encountering the SequelizeEagerLoadingError indicating that one table is not associated with the other, despite trying all the suggested solutions on this platform. The tables in que ...

The REST API in Python encountered an error when attempting to cast a JSONArray to a JSONObject

I'm working on a python script that has two main tasks: Parse a CSV file Send the parsed data to a remote server using its REST API So far, my code successfully parses the CSV file and converts it into a JSON object. However, when I try to send th ...