Arranging JSON API data in chronological order from oldest to newest dates

I've encountered an issue with the FinancialModelingPrep API. When I request data for charting purposes, I need the output to be sorted in ascending order, but it consistently comes back in descending order.

Here is a snippet of the response:

[
  {
    "date": "2023-09-08 15:55:00",
    "open": 178,
    "low": 177.99,
    "high": 178.34,
    "close": 178.19,
    "volume": 2640606
  },
  {
    "date": "2023-09-08 15:50:00",
    "open": 177.93,
    "low": 177.79,
    "high": 178,
    "close": 177.995,
    "volume": 1188267
  },
  {
    "date": "2023-09-08 15:45:00",
    "open": 178.01,
    "low": 177.9,
    "high": 178.14,
    "close": 177.925,
    "volume": 757194
  }...
]

I have attempted using different parameters in the URL like revert=true, but unfortunately, I haven't been able to resolve the issue. FMP has not provided any guidance on how to address this problem.

Answer №1

To arrange the output array in a specific order, you can utilize its sort function and specify the sorting criteria based on the timestamps of each element. Let's assume that the output data is stored in a variable named "output", you can apply the following code:

output.sort((a, b) => new Date(a["date"]) - new Date(b["date"])); // Sorting in Ascending Order
output.sort((a, b) => new Date(a["date"]) + new Date(b["date"])); // Sorting in Descending Order

Make sure to sort the array after receiving the response, and this should help resolve the issue at hand.

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

Enhancing Minecraft's surface world generation to create a balanced distribution of ores

I'm looking to reduce the occurrence of ores in my Minecraft Java Edition server by half in the overworld. I've come across conflicting information about whether this can be done for the regular overworld or only in a custom dimension according t ...

Having trouble getting the jQuery autocomplete feature to function properly?

On my page, there is a button labeled "Add a Skill." Clicking on this button should trigger the display of an input box where you can enter your skill, another input box for your skill level, and a horizontal slider to select the skill level. In my databa ...

Ways to assign a custom name to objects in JSON

When returning JSON from ASP.NET Web API, the JSON is returned successfully. However, there seems to be an issue where at the beginning of the JSON array, there is no object name that can be used in an Android application. Is there a way to set this objec ...

NestJS is throwing an UnhandledPromiseRejectionWarning due to a TypeError, specifically stating that the function this.client.send is

I am in the process of developing a nestJS API that utilizes Express and attempts to establish a connection with rabbitMQ. Everything seems to be at 99% completion, but I am encountering this final issue even after implementing all the necessary code. impo ...

The WebSocket connection to '...' was unsuccessful due to an invalid frame header

I currently utilize express and socket.io for my project. The node server is up and running on 127.0.0.1:3000, and I have successfully established a connection. var socket = io.connect('http://127.0.0.1:3000', {query: 'id=' + user.id} ...

Supporting HTTP PATCH in XmlHttpRequest

I have been working on a todo app and I'm considering using the PATCH HTTP method to add and remove todos as it would be more semantically accurate than PUT. In the backend, I am utilizing express.js (node.js) and in the front-end backbone.js (which ...

Leverage the URL parameter with variables in HTML using AngularJS

I'm facing the same issue as yesterday: trying to extract and utilize parameters from the URL within my HTML page using AngularJS. What am I doing wrong? The parameter I need to work with is: https://url.com/index.html?user I aim to retrieve the "u ...

Generate several JSON files containing two lists utilizing iteration loops

Can anyone guide me on how to create multiple JSON files with two lists? Let's say I have the following input: animal_list = ["cat", "dog", "bird", "fish", "chicken", "tiger"] name_file = [&q ...

Rotating an Object3D in Three.js using an axis rotation technique

I am attempting to change the orientation of a mesh that has been loaded into an Object3D using OBJMTLLoader. var obj = new THREE.Object3D(); // loading process... obj.rotation.y += 0.1; //executed within the update function This method works correctly ...

"Utilizing AngularJS to apply Filter two times within an ng-repeat loop

I recently started learning about angularjs and am currently grasping the concept of filters. However, I have come across an issue where angularjs is calling the filter twice instead of once as expected. I'm quite puzzled by this behavior and unsure w ...

Attempting to input data into elements that have been generated automatically

Trying to set values to elements with automatically generated id's: This code is functional: <input type="tekst" id="<?php echo $item->getId();?>"> <script> document.getElementById("<?php echo $item->getId();?>").v ...

Rendering on the server with React: Utilizing server-side rendering to display product information with passed-in :productId parameters

Having an issue with my React app that is rendered on both the client and server (Node and Express). When someone types in the URL directly, the application crashes because it cannot parse the '1' in the URL to fetch the correct product. If a l ...

Creating a json object using data from an HTML table

I am dealing with HTML returned from a web service that I cannot control. The HTML structure returned by the web service looks like this: <table id="result"> <tbody> <tr> <td><b>Name</b></td> < ...

Using specific sections of JSON data to display in an alert message (Vanilla JavaScript)

I am seeking a bookmarklet that, upon clicking, will access JSON data and retrieve the "temp" information to display in an alert indicating the weather with just one click. Is there a method to achieve this or do I need to utilize a different API? Here&ap ...

Expansive background with adaptable height dimensions

Is there a way to use Foundation to create a header with a full-width image and responsive height using CSS only, or do I need to use JavaScript for this? Can someone assist me with this? This is the code from index.html: <!doctype html> <html ...

Retrieving File Using HtmlUnit Page Response

Currently, I am facing an issue while attempting to utilize HtmlUnit to navigate to a specific URL. Upon visiting the URL, it initiates the download of a JSON file containing relevant data. My query revolves around extracting and processing the data from t ...

Transfer data in JSON format to the server using Ajax

Currently, I have an HTML form where I am in the process of creating a JSON object using JavaScript. Here is how it looks: var JSobObject= '{"name":"'+personObject.GetPersonName()+ '","about":"'+personObject.GetAbout()+ &ap ...

I need help with my calendar that only displays months with 31 days! Can anyone assist me, please?

My calendar was functioning properly earlier. However, it has started to skip months and only display those with 31 days. For example: if the current month is May, the next month should be June. But instead, it repeats May and then jumps to July (which ha ...

Troubleshooting error: Unable to access property 'post' in Angular Service when using $http

As I delve into learning Angular, my approach involves shifting all the business logic to services. However, I encountered an error while attempting a post request within a service: Cannot read property 'post' of undefined Provided below is a ...

What is the best way to retrieve every single element stored in an Object?

On a particular page, users can view the detailed information of their loans. I have implemented a decorator that retrieves values using the get() method. Specifically, there is a section for partial repayments which displays individual payment items, as d ...