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

What could be causing checked="checked" to fail validation in JavaScript?

I'm feeling a bit puzzled about this issue. Here's the HTML code I have - <input type="radio" id="20577091" name="q_engine" value="20577091" style="position: absolute; opacity:0;" checked="checked" /> and here is the corresponding JavaScr ...

Combine multiple values into a single input in the number field

I have a number type input field... What I am looking for is: Age-Height-Weight 20-180-80 Is there a way to ensure that users input data in this exact format and then have the final result inserted into the input field with type="number" and submitted? ...

Navigate through FAQ items with sliders using Owl Carousel 2 - Easily switch between different chapters

Exploring the creation of a FAQ section with individual carousels for each item. My primary objective is for the carousel to transition to the next chapter when advancing beyond the last slide (backwards navigation would be a future enhancement). I'v ...

Issue encountered in Angularjs during upgrade process from version 1.2.27 to 1.4.7

Recently, I upgraded my app from using AngularJS 1.2.27 to version 1.4.7, but now I am encountering an error in the AngularJS file. SyntaxError: Unexpected token : (angular.js:12477) at Function (native) at Object.sd. ...

Leveraging Window Object in Custom Hooks with NextJS

ReferenceError: window is not defined This issue arises on the server side when NextJS attempts to render the page. However, it is possible to utilize window within the useEffect hook by following the guidance provided here. I am seeking advice on creati ...

What is the best way to exclude a specific key when adding JSON objects to an array?

I'm facing a challenge with a JSON array of objects that contains other arrays of objects. The structure of this array is not fixed, making it difficult for me to delete specific elements like delete mainArray[0].obj.subobj[1].objToOmit; In my progra ...

Tips for ensuring that the horizontal scroll bar remains consistently positioned at the bottom of the screen

There are two div sections on a page, with one positioned on the left and the other on the right. The div on the right contains multiple dynamically generated tags which necessitate a horizontal scroll bar (overflow:auto). This causes the div's height ...

Unallocated functions found within Web Sockets Objects

I am currently utilizing javascript (p5.js) in combination with node.js using express and socket.io. Within my code, I believe the issue lies within this specific section: for (var i = 0; i < 3; i ++){ for (var j = 0; j < 3; j ++){ ...

What steps should I take to create a .NET/JavaScript login page that works on IE7, Chrome, Mozilla, and Safari browsers?

I am currently dealing with a .NET login page that functions perfectly in Internet Explorer 7. However, my goal is to enhance its compatibility with Chrome, Mozilla, and Safari. The login code behind looks like this: Protected Sub btnLogin_Click(ByVal se ...

XMLHttpRequest request shows blank result

Issue: After clicking the submit button on my HTML form, a JavaScript function is called with an Ajax request. The request returns successfully, but the result disappears quickly. I'm curious if I may be overlooking something here (besides jQuery, w ...

Does the image alter based on the selection in the dropdown menu?

When I utilize the Templating Example from the select2 library, everything seems to work for the first dropdown value change. The correct image is previewed as expected. However, when trying to change the value a second time, a second image is appended but ...

Choosing one item from an array to showcase in a view [Angular]

I've previously inquired about this issue without success. My current challenge involves a store app created with AngularJS. I am trying to implement a feature where clicking on an item will open it in a separate "item" view, displaying only the info ...

The chosen options are not appearing. Could this be a problem related to AngularJS?

I am working on setting up a dropdown menu in HTML that includes only the AngularJS tag. This dropdown menu will be used to populate another AngularJS dropdown menu. Below is the code for the first dropdown menu: <select class="form-control" ng-model=" ...

Executing a function to erase the stored value in local storage during an Angular unit test

Looking to verify whether the localStorage gets cleared when I execute my function. Component ngOnInit() { // Logging out when reaching login screen for login purposes this.authService.logout(); } authService logout() { // Removing logged i ...

Label dynamically generated from user input for radio button option

In my attempt to develop a radio group component, I encountered an issue where the value of one radio option needs to be dynamically set by an input serving as a label. While I have successfully created similar components before without React, integrating ...

Detecting collisions in Three.js

I seem to be overlooking something quite fundamental, as I can't seem to find a solution in the documentation or any other working code examples. I am currently creating a basic museum using THREE.js libraries. While most of it is set up, I need to im ...

"The error message "Node JS, MYSQL connection.query is not a valid method" indicates

db_config.js: const mysql = require('mysql'); var connection = mysql.createConnection({ host: 'localhost', user: 'root', password: '', database: 'test' }) connection.connect(function(err) ...

In Production environment, v-model triggers a ReferenceError

My Vue View includes the following code: <script setup> import Overwrite from "../components/Overwrite.vue"; </script> <template> <div> ... <textarea v-model="text" cols="99" rows=&qu ...

Cease the loading of a script in an HTML file

I have encountered a challenge in preventing a script from loading on my Wordpress website. The HTML file contains two scripts: <script type="0f1a6d7ca503db410c0d10c4-text/javascript" src='https://www.[-----------].se/wp-content/plugins/ ...

Issue: A request is not pending for flushing during the testing of an AngularJs service

As a beginner in AngularJs, I am currently working on my first unit test. In order to test the service I created, I wrote a test that simply returns a single Json object. However, whenever I run the test, I encounter the error mentioned in the title. I am ...