JavaScript .map() function is failing to return the modified array

Hey there! I have an array of dates that look like this: [2019-02-21T04:06:32.000Z]. I'm trying to convert these dates into the format [02/21/2019 4:06:32 AM] using JavaScript's map function.

dateArray.map( x => {
   return moment.tz(x, 'Etc/UTC').format('MM/DD/YYYY h:mm:ss A').toString()
});

After running this code, when I console log the date array, it still shows the dates in the original format [2019-02-21T04:06:32.000Z], even though inside the map() function they are formatted correctly. Can anyone help me figure out what I'm doing wrong here? Thank you!

Answer №1

.map function does not alter the original array. Make sure to use:

dataArray = dateArray.map( x => {
  return moment.tz(x, 'Etc/UTC').format('MM/DD/YYYY h:mm:ss A').toString()
});

Answer №2

To modify the existing array, it's recommended to utilize a for loop.

dateArray = ['2019-02-21T04:06:32.000Z'];

for(let index = 0; index < dateArray.length; index++){
  dateArray[index] = moment.tz(dateArray[index], 'Etc/UTC')
    .format('MM/DD/YYYY h:mm:ss A').toString();
}

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

The customized message for parsley's minimum length is not being applied properly when passed as an option during binding

When attempting to bind the Parsley script to an input field, I am encountering an issue with the minlength message not displaying my custom one. Instead, the default error message from Parsley is being shown. I've tried adjusting the code but it does ...

Give a CSS Element a specific class

Can all h3 elements be styled with the class responsive-heading using only CSS? Take a look at the CSS snippet provided for more clarity: h3 { .responsive-heading } /* The responsive-heading class is defined in another CSS file and includes: .respons ...

Monitor website visitors' traffic sources using the Google Analytics Nuxt module

Recently I integrated Google Analytics into my Nuxt website. After installing the module, I configured it in the nuxt.config.js file like this: gtm: { id: 'GTM-WGW****' }, googleAnalytics: { id: 'UA-230******-*' ...

Is it possible to retrieve the original array after removing filters in Angular?

Within my component, I have an array that I am filtering based on a search string. The filtering works as expected when the user inputs characters into the search field. However, I am encountering an issue when attempting to display all records again after ...

What is the process for invoking a server-side code behind method using a JavaScript function in the client side?

I have a JavaScript function that is triggered by the click event of an HTML button on an ASPX page. Additionally, there is a server method located in the code-behind page for handling this functionality. My goal is to invoke the server method from the Jav ...

What is the process for casting a two-dimensional array in the C programming language?

Lately, my mind has been feeling quite fuzzy and I'm struggling to recall why the following C code snippet is causing a warning: char a[3][3] = { "123", "456", "789" }; char **b = a; I am receiving the message: warning: initialization from incompat ...

A guide on showcasing a value in an Input box using AngularJS

Recently, I started delving into AngularJS and I am determined to create a user info editing page where the old info is displayed in input fields. The goal is to allow users to easily see and update their information. I previously achieved this without usi ...

The connection between the type of request and the corresponding response in an Ajax function

When using the following: xhr.setRequestHeader("Content-Type", "application/json"); Am I obligated to only receive a response in json format, or is it possible to receive an html response instead? If there is flexibility in receiving different formats, h ...

Develop a program that executes a function across spreadsheets that satisfy specific conditions

I am currently developing an onEdit(e) script that will highlight cells in a Google spreadsheet (specifically in a certain column) after a specified user has made edits. The challenge I face is ensuring this functionality applies to all spreadsheets with a ...

Is the string missing from the array?

Can someone help me figure out how to determine if a value is within an array in PHP? <?php $depts = $db->query("SELECT depts FROM tbl_staff WHERE id = '".$_SESSION['exp_user']['userid']."'"); $department = $dep ...

How can I transmit information to and retrieve information from a node.js server?

When working with React code, I encountered issues with sending 'post' and 'get' requests. After some investigation, it seems that the problems lie within my server-side code. Overall Setup const express = require('express') ...

Angular UI grid: Arranging numbers in a straight line at the decimal point

I am interested in aligning decimal numbers in Angular UI Grid as shown below. 11.293 .89 233424 .34345 I have considered different approaches such as using a cell template with aligned divs or transparent 0s. Has anyone successfully imp ...

I am having an issue where my div element is not inheriting the width of its

I am encountering an issue where I have one div and I check the width of that div in jQuery. I then try to set the same width on the next div, but the width does not appear. Can someone please assist me with this? Here is my code: $(document).ready(funct ...

Having trouble accessing env variables from React Component in Next.js?

I recently set up a Next.js project and included an .env file to store environment variables used in my server.js file. However, I am facing an issue when trying to access these variables from a component. Can anyone provide guidance on how to resolve this ...

The payload is properly returned by the Reducer, however, the component is receiving it as

In my current project, I am developing an application that involves fetching data from a firebase database. This particular database does not require authentication, and I have already adjusted the access rules to allow for public access. One of the actio ...

Retrieve information from a text

I retrieved this data as a string from a webpage using jQuery and need help parsing it. When I attempted to use jQuery.parseJSON, I encountered the error Uncaught SyntaxError: Unexpected token n. The specific values I am looking for are "surl" and "imgur ...

Selecting from dropdown menu showing limited options extracted from XML data

Need help with populating a combo box named combo_assembly using Ajax. The function to achieve this is: function populate_combo_assembly(xmlindata) { var xmldata = xmlindata.getElementsByTagName('Assemblies'); if(xmldata.length <= 0) { ...

Guide to executing Javascript instructions across multiple lines in Selenium Basic

Currently, I'm utilizing VBA Selenium Basic to parse through an HTML document in attempt to modify the attribute style within a td element. Despite my efforts, I have been unsuccessful in finding methods such as webelement.RemoveAtrr, webelement.SetAt ...

JavaScript: unable to locate information within JSON reply

Looking to develop a Twitter bot utilizing the Twitter and Spotify APIs. Making progress, but encountered an issue I can't tackle alone: Seeking the artist and song name of the top 1 track from the top 50 Spotify songs. Upon sending a request to the ...

choose the drop-down option that is consistently set to the first index

I am currently working on a JavaScript code that aims to fetch the selected item's value, but it seems to always point to the 0 index and return the first value. My HTML Code: <html> <head> <title>Simple Extension</title ...