How can I add items to a multi-dimensional array within a loop using JavaScript?

When I parse JSON data, I am retrieving 3 values in each node.

$.each($.parseJSON(data), function (key, val) {
    Var X = val.date;
    Var y = val.type;
    Var z = val.text;
});

An example of JSON data is as follows:

val.date= '2011/02/09', val.type=3, val.text = 'Some text'

I want to store these values in an array like this:

var arrA = new Array();
arrA[0] = new Array(X,Y,Z);
arrA[1] = new Array(X,Y,Z); etc

With X, Y, Z changing for every node in the JSON data. Ultimately, my arrA should contain the following data:

['2011/02/09', 3, 'Some text'],
['2011/12/11', 3, 'something to show']
.
.
.
['2011/02/08,3,'something else']

Can someone suggest the best way to achieve this?

Many thanks, Adarsh

Answer №1

let newArray = [];

$.each($.parseJSON(data), function (index, value) {
    newArray.push([value.date, value.type, value.text]);
}

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

Validation of JSON Failed

Encountered a 400 Bad Request error while attempting to POST an answer using Postman, it appears to be a validator issue. Despite multiple attempts, I have yet to resolve this issue. Below are details of the JSON data being sent in the POST request along w ...

What is the best way to convert a list of proxies into a JSON format of proxies?

I currently have a collection of proxy servers: proxies = [ "188.166.78.120:8080", "18.210.69.172:3128", "178.128.166.50:8118" ] However, the library I'm using requires the proxies to be structured in JSON format like this: proxies = { "htt ...

Retrieve information from a database by utilizing AJAX and store it in a JavaScript array

I'm facing an issue where I can retrieve data from the PHP file, but not from the database to my JavaScript code. I am using Ajax to fetch the data from the database, then passing it to the PHP file, and finally trying to filter this data using JavaSc ...

What is the best way to store objects in files using Node.js?

As I manage my Node server, a question arises - what is the best way to convert objects into a serialized format and save them to a file? ...

Using JSON to submit a form and then interpreting it using PHP

I'm struggling to solve this issue despite trying various approaches. I am aiming to submit form data as JSON and then redirect it to a PHP page where the JSON data is processed. Currently, my setup looks like this but it doesn't seem to work: ...

Tips for retrieving the specific JSON node instance with Groovy?

When working with JSON files in Groovy, it can sometimes be tricky to differentiate between different types of nodes. For example, the "value" and "onclick" nodes in the provided JSON file both return as ArrayLists, even though logically we expect "value" ...

The redirection to the HTML page happens too soon, causing the ASYNC functions to fail in saving the user's image and data to the server

Hey everyone! I'm facing an issue with async/await functions. Here's the code snippet from my backend where I'm trying to save details of a newly registered user. I'm puzzled as to why the Redirect() function is executing before the f ...

Using Vue JS to extract and merge data from an API response in JSON format

Hey there! I'm a new developer working with Vue JS. I have a response body where I need to sum the amount values that have is_paid:true using only the paid_amount. Can someone guide me on how to achieve this in Vue JS? Edit: For example, I want to ad ...

Obtaining fresh data during an ajax update

I've noticed that my code is functioning correctly, but I'm having trouble grasping it completely. Could someone please provide an explanation? Here is the code snippet: <script type="text/javascript"> var portletNamespace = '#& ...

How come the HTML page served by the express.js route isn't linked to a CSS stylesheet?

Recently, I've been working with a server.js file that imports a router const path = require("path"); const express = require("express"); const app = express(); const PORT = 8080; app.use(express.urlencoded({ extended: true })); app.use(express.jso ...

Error: (3) Invalid range specified for column 3 during globbing operation

When attempting to index a basic JSON data in Solr using curl, I encountered an error message. Here is the command I used: "curl -X POST -H 'Content-Type:application/json'-d http://localhost:8983/solr/informationretrieval/update/json/docs ' ...

Converting JSON to TypeScript with Angular Casting

I'm facing an issue detailed below. API: "Data": [ { "Id": 90110, "Name": "John", "Surname": "Doe", "Email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="472d282f2923282207202a262e2b ...

Regular expression that can be used to extract information from a text file by filtering and returning only

When I open a text file, it contains several lines like the examples below. surname australia enter name j.jonhs enter name j.cause enter name f.london enter I am seeking assistance to modify the code snippet below so that it captures the first line m ...

What is the best method for sending a DbGeography parameter to MVC?

I am working on developing an API that allows users to save polygons on the server using ASP.NET MVC 5. Can anyone guide me on how to properly format the AJAX parameters for posting requests with DbGeography? This is what I have tried so far: $.ajax({ ...

Tips for locating the :before element's position with Javascript

I am currently working on an animation where I need to change the color of an element in the center once a small circle touches it. Despite trying to use the position() method, it does not work as intended because I am using a :before for animating the div ...

Utilize JQuery to extract data from JSON and populate form inputs

Working on a web project where I need to make use of AJAX and JQuery to update certain fields without refreshing the entire page. After testing, everything seemed to work fine with dummy data. However, when trying to parse actual JSON data generated from M ...

When an HTML element is deleted, its events are not being removed as expected

I currently have events bound in my backbone view. sampleView = Backbone.View.extend({ events: { "click .sum": "sumButtonFunc", "click .diff": "diffButtonFunc" } sumButtonFunc: function(){ console.log("sum called") ...

Steps to create a slideup effect using JavaScript

Upon clicking the answer text area for the first time, a message box will appear. Your Answer Thank you for contributing an answer to this platform! Please remember that this is a Q&A site, not a forum for discussion. Include specifics and share you ...

JQuery table sorter is unable to effectively sort tables with date range strings

I am facing an issue with sorting a column in my table that contains text with varying dates. The text format is as follows: Requested Statement 7/1/2014 - 9/16/2014 When using tablesorter, the sorting does not work properly for this column. You can see ...

Having trouble with bootstrap carousel malfunctioning on Firefox browser?

I'm experiencing an issue with the carousel slider on my website. It seems to only be working in Chrome and not in Mozilla Firefox. You can view the live site at: Below is the code I have used for the carousel: <header id="myCarousel" class="car ...