Guide on converting a JSON object to an array list in JavaScript

Hey there, I have received JSON object data via AJAX. Take a look at it below:

var history= [
    { date: '12/1/2011', open: 3, high: 5,low: 2, close:4 },
    { date: '12/2/2011', open: 4, high: 6,low: 3, close:5 },
    { date: '12/3/2011', open: 3, high: 5,low: 2, close:3 }
];

I am interested in mapping only the stock data to an array list as shown below.

[
  [3,5,2,4],
  [4,6,3,5],
  [3,5,2,3]
]

If possible, I would prefer to use the map method (data.map(function(a)). Any helpful advice or guidance on this matter would be greatly appreciated. Thank you!

Answer №1

We advise against using the variable name history.

The Window.history read-only property gives you access to the History object, allowing you to manage the browser session history (pages visited in the tab or frame where the current page is loaded).

You can experiment with map(), Object.values(), and slice():

var historyList= [
    { date: '12/1/2011', open: 3, high: 5,low: 2, close:4 },
    { date: '12/2/2011', open: 4, high: 6,low: 3, close:5 },
    { date: '12/3/2011', open: 3, high: 5,low: 2, close:3 }
];

historyList = historyList.map(o => Object.values(o).slice(1));
console.log(historyList);

Answer №2

It appears that you are already well-versed in this topic :)

let records= [
    { date: '1/1/2019', open: 10, high: 15, low: 5, close:12 },
    { date: '1/2/2019', open: 12, high: 18, low: 8, close:14 },
    { date: '1/3/2019', open: 11, high: 17, low: 6, close:13 }
];

const extractData = item => [item.open, item.high, item.low, item.close];
const result = records.map(extractData);

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

Exploring the capabilities of accessing multi-layered arrays in PHP

Trying to print a multidimensional array obtained from an API, the objective is to iterate through and determine if the field [Percents] exceeds 20. However, my attempts have been unsuccessful. Here is what the array looks like: View Multidimensional Array ...

JavaScript Object's Data Returns as Undefined

Trying to retrieve data from mongoDB using the find() function, which should return objects in an array format. However, encountering an issue where the desired data is not showing up in the array and instead returning undefined. Here is the Object Array: ...

Using double quotes and single quotes within a JSON string can sometimes cause conflicts and

I'm trying to understand the difference between two JSON Strings: <!DOCTYPE html> <html> <body> <h2>JSON Object Creation in JavaScript</h2> <p id="demo"></p> <script> var txt = '{"name":"Jimmy", ...

display alternative text before an image is loaded on the screen

Is there a way to display text instead of an image before the image loads? I have a website with an image as the title, causing the page to load fully before the title image is loaded later. Is there a solution for this issue? ...

Eliminate deeply nested JSON object using rapidjson library

I have been struggling to find a solution for removing an object nested within another object in a JSON file. Despite searching extensively online and on the official rapidjson page, I haven't come across any relevant examples or resources. The code I ...

Could you provide instructions on how to change mjs files into js format?

Is there a method to change .mjs files to .js files? Some hosting services do not yet support mjs files, so I am interested in converting them to js files. Context: Mozilla's PDFjs has incorporated JavaScript modules (mjs) into their code, but since ...

Is the ID Column in the Minimal Material Table Demo not appearing as expected?

Hey there, I'm in the process of developing a simple demonstration of a material table - Take note that this is a stackblitz link and for some reason, the id column isn't showing up. Here's a snippet from my app.component.ts: import { C ...

Is it necessary to delay until the entire page finishes loading in Selenium 3?

public boolean CheckPageLoadStatus(){ final ExpectedCondition<Boolean> pageLoadCondition = new ExpectedCondition<Boolean>() { public Boolean apply(final WebDriver driver) { return ((JavascriptExecutor) driver).executeScr ...

Firebase Storage Metadata Disappearing Act

Despite trying to add custom metadata to my image in Firebase, it doesn't seem to be accepting it. I expect to see my metadata listed under 'Other Metadata' after I upload the image. Currently, this is how it appears for me: https://i.sstat ...

The height of the image will be fetched only once from a large collection of images

As mentioned in the title, I have multiple elements with the same class and I am trying to fetch that class to check for the width/height/src of the child image. I am only able to retrieve the height and width of the first image, but I can get the src of ...

Encountering an issue when transferring data between controllers utilizing a demo service

During my journey of learning AngularJS, I decided to create a small app that would allow data to be passed between two controllers using services. Below is the code snippet that showcases how I achieved this: The Controller Code <!DOCTYPE html> &l ...

"Eliminating Redundant JS and CSS Files in Magento to Improve

While analyzing the performance of a website, I noticed that GTmetrix found some CSS & JS files being loaded from different locations but containing the same file. An example: <action method="addItem" module="ves_blockbuilder" ifconfig="ves_blockbuilde ...

What is the most effective way to accurately identify the mobile platform of a user using JavaScript?

I need to determine whether a user has accessed the application through a browser on Android or iOS. The company would like to display slightly different content depending on the platform. While I am aware that navigator.platform can be used for this pur ...

What is the process for adding images from CSS (backgrounds, etc.) to the build folder with webpack?

Trying out the file loader for processing images and adding them to my build folder. Images within HTML files successfully show up in the build, but the ones from styles do not. I've divided my webpack configuration into two separate files and use t ...

Accessing multiple JSON files

Upon inspecting the browser console, I found this: https://i.sstatic.net/Rjwjm.png The AJAX function responsible for this is as follows: function displayFiles(){ var classElements = document.querySelectorAll("table.folders-list tr.ui-selected td span"); ...

Angular 9 Alert : TypeError: Unable to access property 'routes' as it is undefined

Currently, I am in the process of learning Angular 9 and experimenting with new features. Recently, I decided to explore router-outlets with a name property which is demonstrated in the code snippet below. This is the template I used: <router-outlet n ...

Looking at NumPy arrays from a new perspective of a varied data type

Imagine having a large NumPy array with a dtype of int32 import numpy as np N = 1000 # (large) number of elements a = np.random.randint(0, 100, N, dtype=np.int32) Now suppose you want the data to be uint32. You could achieve this by b = a.astype(np.uint3 ...

My Angular directive appears to be missing from the page

Using Electron and AngularJS to implement basic functionality. Currently adding a header and footer that will be displayed on every page. Here is the structure in app.js: 'use strict'; let app = angular.module('marks', ['ngRoute ...

unable to utilize react-speech-recognition package

Looking for a simple react package that can convert user audio to text? I recently installed one and tried its basic code example, only to encounter an error message stating "RecognitionManager.js:247 Uncaught ReferenceError: regeneratorRuntime is not defi ...

Adding properties with strings as identifiers to classes in TypeScript: A step-by-step guide

I am working with a list of string values that represent the identifiers of fields I need to add to a class. For instance: Consider the following string array: let stringArr = ['player1score', 'player2score', 'player3score' ...