Click on the buttons to choose the data, then merge the corresponding CSV files into a single array

I'm currently developing a user interface where individuals can select CSV names by clicking on corresponding buttons to visualize the combined data in a chart.

The setup involves adding the selected CSV names to an array (referred to as chosenData) with each button click. Subsequently, a for loop is used to iterate through the chosenData array, fetch the data from GitHub, and append all data into another array named allData.

Unfortunately, there seems to be an issue with the data not merging correctly. I've spent hours trying to troubleshoot this problem without success, so any assistance would be greatly appreciated!

Find the relevant code below, and you can also access the jsfiddle for reference.

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="//code.jquery.com/jquery-1.10.2.js"></script>
    <script src="https://d3js.org/d3.v4.min.js"></script>
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>

    <title>combine CSVs</title>
</head>

<body>
    <button id="arr1" class="button">arr1</button>
    <button id="arr2" class="button">arr2</button>
    <button id="arr3" class="button">arr3</button>
    <button id="arr4" class="button">arr4</button>
    <button id="arr5" class="button">arr5</button>
    <script>

                var parseTime = d3.timeParse("%Y-%m")

        let chosenData = []
        let allData = []

        $('.button').on('click', d => {
            let data = event.target.id
            console.log(data)
            chosenData.push(data)
            console.log('chosenData', chosenData)
            obtainData(chosenData)
        })

        function obtainData(chosenData) {
            for (i = 0; i < chosenData.length; i++) {
                let arrayName = chosenData[i]
                let dailyData = axios.get("https://raw.githubusercontent.com/sprucegoose1/sample-data/main/" + chosenData[i] + ".csv")
                    .then(content => {
                        let single = content.data
                        let singleCSV = d3.csvParse(single)

                        singleCSV.forEach(d => {
                            d.value = +d.value
                            d.interval = +d.interval
                            d.time = +d.time
                            d.code = arrayName
                            d.date = parseTime(d.date);
                        })
                        console.log('single data', singleCSV)

                        allData.push(singleCSV)
                    })
            }
            const merge = allData.flat(1);
            chartData(merge);
        }

        function chartData(allData) {
            console.log('all data', allData)
            // create a chart with combined data here
        }

    </script>
</body>

</html>

Answer №1

It seems like the issue lies in the asynchronous nature of the fetches. While you initiate them within the for loop, you fail to wait for their completion before proceeding with flattening and calling chartData.

A potential solution could involve storing the promises returned by axios.get into an array during the loop iteration, followed by utilizing Promise.all after the loop to ensure that all fetches have been resolved before merging.

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

Loopback Model Property Somehow Resurrected After Deletion

Within my function, I am working with an object called 'ctx.instance' that contains various properties: firstName: 'Ron', lastName: 'Santo', minor: true, accepted: false, emailChanged: false, organizationId: 00000000000000000 ...

The jQuery Deferred feature on Ajax is failing to properly pass the array in the data option as an array, instead converting

I am facing an issue in my application where I want to use jQuery deferred to handle Ajax success and error uniformly from a central location. It works perfectly fine when I pass strings in the data option, but when I try to pass an array, it gets sent as ...

Select dropdown in AngularJS for Date formatting

After retrieving json data from a web API, I found that it contains a series of datetimes. My goal is to select a specific datetime from a dropdown list. While I can populate the list without any issues, the formatting appears to be incorrect and I'm ...

Use jQuery to dynamically capture and store the value of data-rowindex into an array

Is there a way to dynamically store the data-rowindex value into an array? <tr class="ewTableRow" data-rowindex="1" id="r1_assessment_training" data-rowtype="2"> Below is the code I've attempted. Not entirely sure if it is the correct approach ...

Using javascript to extract values from nested JSON structures without prior key knowledge

My JSON data is structured as follows: {"sensors": {"-KqYN_VeXCh8CZQFRusI": {"bathroom_temp": 16, "date": "02/08/2017", "fridge_level": 8, "kitchen_temp": 18, "living_temp": 17, ...

Creating functionality with a native JavaScript plugin within a directive and test suite

I have a custom JavaScript plugin that is integrated within a directive and utilized in an Angular manner. Snippet of the directive, export default function () { 'use strict'; return { restrict: 'E', scope: { map: &apo ...

Transforming button properties into a JSON format

I am currently in the process of developing a web application using node.js and mongodb. Within my app, there is a table that dynamically populates data from the database using a loop. I encountered an issue with a delete function that I implemented base ...

Issue with MongoDB: mongoimport not recognizing empty field value at the beginning of a tsv file

Running mongodb version v1.8.0 on Mac 10.6.8 has presented me with a challenge. My attempt to execute a mongoimport on a TSV file seems to be causing some issues. Despite not using the --ignoreBlanks switch, it appears that blank fields are being disrega ...

What are the necessary components for the package.json file of my npm package in order to easily install it within a different project?

I am currently in the process of creating a compact react component that can be easily implemented into other projects. Here is the content of my package.json file so far: { "name": "my-package", "version": "0.1.1&qu ...

Generating an in-page anchor using CKeditor 5

We are currently integrating CKEditor 5 into one of our projects. Our goal is to enable the end-user to generate an in-page anchor tag that can be accessed through other links (e.g., <a name='internalheading'> which can be navigated to via ...

Leverage the power of Meteor by incorporating templates to dynamically inject HTML based on an event

I am currently generating HTML using JQuery on an event. However, I believe a better approach would be to use templates in Meteor, especially if the HTML becomes more complex. Template.example.onRendered(function() { paper.on({ 'cell:mous ...

What methods can be utilized to create sound effects in presentations using CSS?

Let us begin by acknowledging that: HTML is primarily for structure CSS mainly deals with presentation JS focuses on behavior Note: The discussion of whether presentation that responds to user interaction is essentially another term for behavior is open ...

Issue with Webpack failing to build ES6 class due to configuration error

I'm running into a problem with integrating webpack into my current React project. Previously, the project was server-side rendered using next.js. However, I seem to be encountering an issue with my webpack configuration. Every time I attempt to build ...

Can someone please help me understand what mistakes I'm making in my array manipulation within a foreach loop?

I have an array called $photos with the following data: Array ( [0] => Array ( [fileURL] => https://www.filepicker.io/api/file/UYUkZVHERGufB0enRbJo [filename] => IMG_0004.JPG ) [1] => Array ...

Having trouble with jQuery validation: Seeking clarification on the error

I have implemented some validations on a basic login page and added jQuery validation upon button click. However, the code is not functioning as expected. I have checked the console for errors but none were displayed. Here is the code for your review: ...

Digital Repeater and Angle Measurer

Seeking Guidance: What is the recommended approach for locating the Virtual Repeaters in Protractor? A Closer Look: The Angular Material design incorporates a Virtual Repeater to enhance rendering performance by dynamically reusing visible rows in the v ...

Manipulating arrays and troubleshooting Typescript errors in Vue JS

I am attempting to compare the elements in one list (list A) with another list (list B), and if there is a match, I want to change a property/field of the corresponding items in list B to a boolean value. Below is the code snippet: export default defineCo ...

Deactivate schema validation in Fastify

I'm in search of the most effective method to programmatically disable json-schema validation for all routes within a fastify instance: module.exports = async (fastify, opts) => { fastify.get('/stations.json', {schema: opts.schema}, asy ...

Unable to retrieve JSON data from the remote URL

I have been attempting to retrieve information from but unfortunately, I am not receiving any data in return. Despite this, I can see the data in my response tab using Google Chrome. My attempts include running it on both a webserver and locally for test ...

How can I call a Vue component method from a different application?

I'm facing an issue where my code works perfectly in pure html/javascript, but not when using vuejs. The function causing the problem is called myMethod(). While debugging in the javascript console, I can access it using myMethod("toto"). However, in ...