Guidelines for populating data with an array

I have the following array:

const dataArr = [[15, 14, 5], [16, 10, 2], [17, 6, 13], [18, 4, 8], [19, 7, 4]];

Now, I am populating another array using the above data as shown below:

for (let i=0; i<dataArr.length; i++){
    newData.push(dataArr[i]);

Then, I want to create a bubble chart using this array but it doesn't seem to work. Here is what I tried:

series: [{
        data: [
            for(let i=0; i<newData.length; i++){
                { x: newData[i][0], y: newData[i][1], z: newData[i][2] }
            }
        ]
    }]

However, this approach is not yielding the desired result. Can someone suggest a way to achieve this?

Answer №1

Start by initializing your data

var arrayAUX = [[ 13, 12,5],[ 13, 10,2],[ 13, 5,10],[ 14, 2,5],[ 14, 3,2],[ 15, 1,2]];
arrayGRAPH = [];
for (var i=0; i<arrayAUX.length; i++){
        arrayGRAPH.push(arrayAUX[i]);
}
var data = [];
 for( var i=0; i<arrayGRAPH.length; i++){
           data.push( { x: arrayGRAPH[i][0], y: arrayGRAPH[i][1], z: arrayGRAPH[i][2] })
        }

After initializing the data, proceed to set it:

series: [{
        data: data
    }]

To see this in action, check out this fiddle http://jsfiddle.net/homdn9md/

Answer №2

If you want to iterate through an array and add its elements to a data structure, you can utilize the forEach method.

var newArray = [[ 10, 5,3],[ 12, 9,2],[ 15, 3,7],[ 16, 8,1],[ 17, 4,6],[ 20, 2,3]];
var info = {infoArray: [{values: []}]}

newArray.forEach(function(element) {
  info.infoArray[0].values.push({ xValue: element[0], yValue: element[1], zValue: element[2]});
});

console.log(info);

Answer №3

The sample code you provided can be optimized by eliminating the arrayGraph array and directly creating an object with the data from the arrayAUX array.

var arrayAUX = [
  [13, 12, 5],
  [13, 10, 2],
  [13, 5, 10],
  [14, 2, 5],
  [14, 3, 2],
  [15, 1, 2]
];

var graph = [];
for (var i in arrayAUX) {
  graph.push({
    x: arrayAUX[i][0],
    y: arrayAUX[i][1],
    z: arrayAUX[i][2]
  })
}

To assign your graph object correctly, do the following:

series: [{
  data: graph
}]

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

I'm having trouble getting jquery css to function properly in my situation

I am trying to implement a fallback for the use of calc() in my CSS using jQuery CSS: width: calc(100% - 90px); However, when I tried running the code below, it seems like the second css() function is not executing. I suspect that there might be an issu ...

Limiting the number of times a specific character appears using a regular expression

Currently, I am in search of a regular expression that allows me to set a limit on the number of times a special character can appear. For instance, if I want to restrict the occurrence of * to a maximum of 5 times in the entire text, then the output shou ...

Encountering difficulty in creating a Nuxt website using @googlemaps/js-api-loader

While using the @googlemaps/js-api-loader in my Nuxt 3 website, I encountered an issue. Everything worked perfectly during local development. However, when I attempted to build the project with nuxt generate (whether locally or on Vercel), I received the f ...

Dynamic styling updates on page refresh in Next.js

There is a strange issue with my styling that I can't seem to figure out. I have a NavBar set to be 20vh in height and an image set to be 100% in width. However, whenever I refresh the page, the NavBar height decreases and the image width increases si ...

Positioning an element in the center of another using JQuery

Greetings! I am currently working with some elements that look like this: <div id="one">content</div> <div id="two">content</div> These elements are followed by another set of elements (without any parent, placed directly after th ...

What is the best way to keep calling an AJAX function until it receives a response from a previous AJAX call

I am looking to continuously call my ajax function until the previous ajax call receives a response. Currently, the page is loading every second and triggering the ajax function, but I want it to keep calling the ajax function until the previous one has c ...

Deactivate the underscore and include the fiscal year in AngularJS

I am currently faced with a scenario where the back end is returning the value as follows: 123222_D1.123 However, I need to display the date from the database (12-Jun-2020) as 2020-D1.123 in a drop-down menu. Currently, I am displaying the above value i ...

Showing the values of selected checkboxes in a select dropdown - here's how!

Link to the jsfiddle : https://jsfiddle.net/a1gsgh11/9/ The JavaScript code seems to not be working on the js fiddle platform. My main concern is that I would like the selected checkbox values to display immediately without needing to submit any buttons. ...

Is it possible for me to determine whether a javascript file has been executed?

I am currently working with an express framework on Node.js and I have a requirement to dynamically change the value (increase or decrease) of a variable in my testing module every time the module is executed. Is there a way to determine if the file has ...

What is the URL I need to visit in my browser to monitor updates while running npm?

I am interested in utilizing npm to monitor any changes made in my project and immediately view them in my browser. Essentially, I have been implementing npm using this modified code snippet from this source, which allows me to run the command npm run buil ...

Cross-Origin Resource Sharing problem: "Preflight request response does not meet access control criteria"

I'm currently tackling a Vue.js/Nuxt.js project that involves sending API requests to 'https://app.arianlist.com'. However, I've encountered some CORS challenges and came across this error message: "Access to XMLHttpRequest at &ap ...

The Next.js application encounters a crash when trying to integrate Google Firebase authentication

I'm encountering an issue while trying to integrate authentication using firebase (via Google) in my next.js application, and the app crashes consistently. I will provide the code for the auth.js page component, as well as where I set up firebase and ...

Categorize elements in an array based on a specific keyword

Struggling with my AngularJS (1) application, I can't seem to figure out how to split an array of items into separate arrays grouped by item. In simpler terms, I have an array with different items and I want to group them by their uuid like this: [ ...

Jasmine for testing HTTP POST method in a unit test case

I am struggling to write a unit test case for the post method in an Angular service. I keep getting an error saying $http is undefined. Below you can see my code. Can anyone please help me figure out what I am missing? I am adding the module using a separ ...

Revert the Vue State When Navigating Back in the Browser

Background In our checkout process, we redirect to Stripe after creating an order object through an API request. To prevent users from clicking the "checkout" button multiple times during this process, we toggle a variable value to show a loading icon whe ...

Error encountered when attempting to assign a value of the original data type within the Array.reduce function

I am in the process of developing a function that takes a boolean indicator object like this: const fruits = { apple: false, banana: false, orange: false, mango: false, }; Along with an array such as ['apple', 'orange']. The go ...

Performing function in Vue.js when a change occurs

I recently started developing a Vue.js component that includes an input field for users to request a specific credit amount. My current goal is to create a function that will log the input amount to the console in real-time as it's being typed. Ultima ...

Construct a structure containing the key/value pairs of cells within an HTML table row using JavaScript

I'm completely new to JavaScript so please be patient with me; I'm not even sure if I'm searching for the solution correctly. Despite spending hours googling and experimenting, I still can't get it to work. My issue is related to an HT ...

Reactjs: When components are reused, conflicts may arise in UI changes

I'm currently working on creating a sample chat room application using reactjs and redux for educational purposes. In this scenario, there will be 3 users and the Message_01 component will be reused 3 times. Below is the code snippet: const Main = Re ...

What is the best way to trigger the keyup event in that moment?

Currently, I am using regex to validate the name field. Each time a key is pressed, a validation function is triggered. If the input matches the regex pattern, I need to empty out that specific character from the input field. However, when previewing the p ...