Forming a multidimensional array based on the values extracted from a JSON file

I am struggling with creating a for loop to return an array from a JSON file named data.json. Here is the desired array structure:

var arr=[["CR7012","",100,1,100],[CR7012,"bla bla bla",100,5,500],["CR7012,"bla bla bla",100,1,100]]

The content of data.json is as follows:

{ "prodotti": [ { "articolo": "CR7012", "descrizione": "", "prezzo": 100, "quantita": "1", "totale": 100 }, { "articolo": "CR7012", "descrizione": "bla bla bla", "prezzo": 100, "quantita": "5", "totale": 500 }, { "articolo": "CR7012", "descrizione": "bla bla bla", "prezzo": 100, "quantita": "1", "totale": 100 } ], }

I appreciate any assistance provided. Thank you.

Answer №1

This is the code that I implemented:

    $(document).ready(function() {
        $.getJSON("./tmp/data.json", function(data) {
                $.each(data.products, function(index, item){
                    var array = [];
                        var productInfo = [item.description, item.price, item.quantity, item.total];
                        array.push(productInfo).toString();
                        });
        });
    });

I then stored the variable array in

var tableData = [array]

in order to create a table using pdfmake, but unfortunately an error was returned.

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

What is the mechanism behind JQuery Ajax?

Currently, I am attempting to utilize JQuery Ajax to send data to a Generic Handler for calculation and result retrieval. Within my JQuery script, the Ajax request is contained within a for loop. The structure of the code resembles the following: function ...

Tips for postponing the first button event in Vue 3 and JavaScript

My current task involves setting up a button event in Vue 3 that triggers a setTimeout countdown on the button before redirecting to another page. The event function has a conditional statement that initiates a countdown from 5 to 0 as long as the countVal ...

Utilizing Django's URL dispatcher dynamically in JavaScript

Im attempting to implement a dynamic url dispatcher as follows: "{% url 'url_name' 'variable' %}" where variable is generated dynamically in my javascript. My goal is to redirect to another page when the value of a <selec ...

Looking to update the location of an element within a canvas using Vue and socket.io?

I am currently developing a 2D pong game using vue.js and socket.io. At the moment, I have a black rectangle displayed in a canvas. My goal is to make this rectangle move following the cursor of my mouse. The issue I am facing is that although my console l ...

Unable to update cube textures at runtime using Three.js r59

I managed to create a cube using the canvas renderer with r59 of Three.js, and it has different textures on different sides, which renders fine. I can also animate this cube's position and rotation without any issues. Now, here's what I'm t ...

Executing a JQuery AJAX request to eliminate a script tag

When using jQuery to call a page via ajax, I encounter an issue with jquery functions loading inside the returned HTML. The problem arises when trying to include a JS file within the returned data, as it seems to strip the script tag: function buildDispla ...

Extract each line of text from the ul li tags at every level of the hierarchy with the help of

I am looking to extract a list of strings from the ul and li elements with slash (/) separating each level up to the nth level using JavaScript. I attempted to use both the Jquery.map and each functions, but unfortunately, I was unsuccessful. ...

Incorporating a transparent icon onto an HTML background

I'm struggling to place a transparent white envelope icon on a green background. I don't understand why it's not working, especially when the telephone icon worked fine. Side Question.: Any recommendations for how to add something above the ...

Error: Attempting to update the value of 'ordersToDisplay' before it has been initialized in a re-render of React. This results in an Uncaught ReferenceError

Trying to dynamically update the document title to include the order number by clicking a button to display different numbers of orders from an array on the screen. The process involves importing a JSON file, filtering it based on user input, calculating ...

Node.js - Locate a specific parameter within an array that is nested within a JSON object, and retrieve the remaining

I am working with a Node.js API call that returns a JSON object. My goal is to extract the customer's phone number from this object using a specific function, like body.result.reservations[X of reserv.].client.phone, and retrieve parameters such as p ...

What is the best way to handle an OR scenario in Playwright?

The Playwright documentation explains that a comma-separated list of CSS selectors will match all elements that can be selected by one of the selectors in that list. However, when I try to implement this, it doesn't seem to work as expected. For exam ...

Having trouble interpreting bar chart with d3js v7. Issue: <rect> attribute height not meeting expected length, displaying as "NaN"

Hello amazing community at Stack Overflow, I've been working on a project for my university where I am trying to create a bar chart using d3v7. I have been following the example from the Blocks but I keep encountering an error mentioned in the title ...

What are the semantics behind implementing Basic Authentication in AngularJS and Spring Boot?

Currently going through a tutorial that involves AngularJS and Spring Security. The AngularJS navigation controller triggers an authenticate function upon page load: var authenticate = function(credentials, callback) { var headers = credentials ? { ...

Choose up to three elements using jQuery

DEMO I'm currently working on a project where I need to select only 3 items at a time. However, all the elements are being selected instead. Can someone please provide guidance on how to achieve this? "If a user wants to select another element, th ...

A guide on how to truncate decimal places dynamically using the toFixed method without rounding

I've been searching for a solution to this particular issue, but it seems that there isn't a similar case on Stack Overflow where the decimal precision needs to be adjustable. Currently, I'm working on a React cryptocurrency project and I wa ...

Establish a WebSocket connection via Meteor.js

How do we establish a Websockets connection in Meteor? Can we achieve this using the following code: ws = new WebSocket('ws://localhost/path'); ws.on('open', function() { ws.send('something'); }); ws.on('message&apo ...

Sending information from an Android device to a PHP server

What is the best way to transfer data from an Android device to a PHP server? I have a GPSTracker.java class that retrieves the current location, and I want to send this location data (latitude, longitude, and address) to the server. What steps do I need ...

An iteration in Javascript/NodeJS using the forEach loop

I am working with a forEach Loop in my code: <% users.forEach(function(user) { %> <tr> <td><%= user.studio %></td> <td><%= user.name %></td> <td><%= user.email %></td> ...

Writing in Node.js involves setting up a local DynamoDB for local development rather than running it in an actual Lambda

This straightforward aws.js script is used to execute dynamoDB operations locally. "use strict"; const AWS = require("aws-sdk"); AWS.config.dynamodb = { region: "eu-west-2", endpoint: "http://localhost:8000& ...

What could be causing Vue Router to only load after I click a link and then refresh the entire webpage?

I have a file called components.js filled with Vue.component instances. Then I have a main.js where I implement a router using these components to create different pages. In index.html, I have links to the router pages. The issue is that on the initial ...