Turning an Array of Objects into a typical JavaScript Object

Below are arrays of numbers:

var stats = [
    [0, 200,400], [100, 300,900],[220, 400,1000],[300, 500,1500],[400, 800,1700],[600, 1200,1800],[800, 1600,3000]
];

I am seeking guidance on how to transform it into the JavaScript object format shown below.

var stats = [
    {x:0, y:200,k:400}, {x:100, y:300,k:900},{x:220, y:400,k:1000},{x:300, y:500,k:1500},{x:400, y:800,k:1700},{x:600, y:1200,k:1800},{x:800, y:1600,k:3000}
];

Answer №1

If you want to transform the elements of an array, you can use Array.prototype.map:

data = data.map(function(item) {
    return { name: item[0], age: item[1], gender: item[2] };
});

The expected result is not in JSON format but rather a regular JavaScript object. To convert it into JSON, simply use the JSON.stringify method.

Answer №2

One way to modify the data is by utilizing the map() method

var info = [
    [0, 200,400], [100, 300,900],[220, 400,1000],[300, 500,1500],[400, 800,1700],[600, 1200,1800],[800, 1600,3000]
];

info = info.map(function(data) {
  return {
    x: data[0],
    y: data[1], 
    z: data[2]
  };
}); 

console.log(info);

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

Firefoxx unable to communicate with server through ajax requests

My dilemma involves transmitting data to my server using the json data type with ajax. Oddly enough, in Firefox the server fails to receive any data at all, while in Chrome and IE the data is successfully transmitted and displayed on the server console. H ...

Convert the xs:choice construct into a JSON schema

Trying to grasp the concept of JSON Schema's oneOf. I am working on converting an XML format into a JSON version and need a JSON schema to validate the key aspects (acknowledging there will be variations). I have developed an XML Schema structure wh ...

The Directive cannot be refreshed as a result of the ongoing "$digest already in progress" error

Within my controller, I have set a default value for a variable called "data". In my original project, I am using CouchCorner to retrieve data from a CouchDB and update the value of this variable. Inside a directive, I am watching the variable data and up ...

What is the best way to send an array to php from a python script?

Utilizing a Python script within my PHP code has proved to be quite beneficial: <?php $command = escapeshellcmd('<path>/myscript.py'); $output = shell_exec($command); echo $output; ?> However, the current challenge I am facing inv ...

Sending data via an AJAX POST request in the request parameter

I have a question regarding the method of making a POST request in my code: $.ajax({ url :"/clientCredentials.json", type: "POST", data: { "clientEmail": email, "clientName":clientName, "orgName":orgName, "l ...

Adjust the TextArea content according to the quantity of selections made in the listbox

If I have a listbox alongside a textarea: <textarea id="MyTextArea"></textarea> <select id="SelectList" multiple> <option value="volvo">Volvo</option> <option value="saab">Saab</option> <option value="me ...

Showcasing JSON information in a Java ComboBox

I successfully retrieved JSON data and displayed it in a table using the code below. Now, I want to show the data in a combobox instead. Currently, the items in the dropdown are being displayed in this format: ljava.lang.string @5c647e05 Can someone plea ...

Downloading a zip file using PHP works successfully when initiated directly, but encounters errors when attempted through a web application

I have been working on a PHP script that generates a zip file and allows it to be downloaded from the browser. The download function in the code snippet below: download.php // ensure client receives download if (headers_sent()) { echo 'HTTP head ...

Is it possible for GSON to perform deserialization in a case-insensitive manner?

While prototyping the communication between a .NET desktop app and a Java server using REST with JSON posts, a case-sensitivity issue has arisen. The .NET objects have their properties in Pascal Casing (as is standard for .NET), such as: Symbol, EntryValue ...

Loop through a non-array or non-object / handling both arrays and non-arrays equally

Sometimes, I find myself needing to handle arrays and single objects in a similar manner. For instance, I may have an object property that can be either an array or just a string (like the scale property): [ { "name": "Experiment type14", "id": ...

AngularJS directive failing to trigger event binding within the link function

Here is a plunker that you can refer to. In my project, I have developed two simple element directives, named incButtonOne and incButtonTwo. These directives are designed to track and display the number of times they have been clicked. Each directive has ...

Having trouble constructing the JSon for ZigBee devices

Due to the lack of APIs, support, and other resources from commax, I am attempting to manually add unsupported devices to build my IoT home. However, I am currently facing difficulties in managing Zigbee dimmers with these devices. What seems to be the iss ...

JavaScript - Delayed Text Appearance with Smooth Start

I want to create a landing page where the text appears with a slight delay - first, the first line should be displayed followed by the second line. Both lines should have an easing effect as they appear. Below is a screenshot of the section: https://i.sst ...

Unusual behavior involving the selection of $stateParams

Seeking a solution for updating angular-ui route parameters based on select field changes. Issue: The route successfully updates with the selected parameter, but the select field does not reflect the change in option selection. Check out the Plunkr. Clic ...

Looking to conduct date comparisons within angular-ui-grid?

I'm currently working on an application that utilizes angular-ui-grid to display form data. One issue I'm facing is how to compare the submission date obtained from an API call with the current date within ui-grid, in order to calculate the numbe ...

Slow CSS :hover animations in the most recent release of Chrome

I recently upgraded my browser from chromium version 67 to the latest Chrome version 79. However, I noticed that after the upgrade, the CSS transitions on my website have become very laggy and unresponsive. Surprisingly, everything works perfectly fine on ...

Error: The document is unable to detect any input values

I've been struggling with this issue for quite some time now and I can't seem to figure it out, no matter how hard I try. It seems that my input field is not capturing the value entered by the user for some unknown reason. Let me share the code ...

Troubleshooting problem with $http in AngularJS: encountering challenges with HTTP JSONP requests

I encountered the following error message while attempting to utilize the JSONP method in AngularJS: Uncaught SyntaxError: Unexpected token : http://example.com/getSomeJson?format=jsonp&json_callback=angular.callbacks._0 Could someone please ass ...

Chrome is having trouble loading basic JavaScript

Here's the JavaScript code I have placed inside the head tag of my HTML: <script type="text/javascript"> function over1() { var img1 = document.getElementById("1").src; document.getElementById("big").src = img1; } function out() { ...

When the browser's back button is clicked, no action occurs with Next/router

I am confused about why my page does not reload when I use the browser's back button. On my website, I have a catalog component located inside /pages/index.js as the home page. There is also a dynamic route that allows users to navigate to specific p ...