Is it possible to convert an object into an array using JSON?

I'm feeling completely lost right now. I've spent hours trying to solve this issue, but every attempt has been a failure. It's almost like my vision is failing me too. I really hate admitting when I need help, but at this moment in time, I don't have any other choice.

The json-response I received from a page is structured like this:

[Object { timestamp="2015-01-04 21:05:16", value="25.4"},
Object { timestamp="2015-01-04 21:10:27", value="25.3"},
Object { timestamp="2015-01-04 21:15:38", value="28.7"},
Object { timestamp="2015-01-04 21:20:49", value="33.5"}]

What I actually need it to look like is this:

[ [1183939200000,40.71],
[1184025600000,40.38],
[1184112000000,40.82],
[1184198400000,41.55],
[1184284800000,41.18],
[1184544000000,41.06]]

I've tried a variety of approaches and I feel embarrassed having to ask for assistance on what should be a simple task.

Your help is greatly appreciated!

Edit:

Upon further investigation, the json response I'm receiving looks like this:

[{"timestamp":"2015-01-04 21:05:16","value":"26.9"},
{"timestamp":"2015-01-04 21:10:27","value":"27.1"},
{"timestamp":"2015-01-04 21:15:38","value":"24.8"},
{"timestamp":"2015-01-04 21:20:49","value":"21.4"},
{"timestamp":"2015-01-04 21:26:01","value":"19.6"}]

When I console.debugged it, I got the "object" form. I'm not sure if this detail makes a difference or not.

Thank you once again.

Answer №1

If we assume that the second element in the array should match the value property of the object:

arr = arr.map(function(obj) {
    return [
        Date.parse(obj.timestamp.replace(' ', 'T')),
        +obj.value
    ];
});

The Date.parse method transforms a string such as "2015-01-04T21:05:16" into a JavaScript timestamp following the ISO 8601 format. This is why a slight adjustment to your current format is necessary.

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

How to query for a timestamp in a specific range in MySQL

I'm facing an issue with a query on tables table1 and table2. I need to retrieve timestamps from table2 that fall between two consecutive timestamps in table1. How can I construct this query? table1: id timestamp 1 2012-08-15 01:11:11 ...

How to Validate Prop Types in VueJS When Dealing With NULL and 'undefined' Values?

Despite what the official VueJS 2 documentation on prop validation says in a code example comment: // Basic type check (null and undefined values will pass any type validation) I encountered an error while testing this piece of code — could you explai ...

SQL command could not be completed

After running the following query: INSERT INTO shop.product(prod_id,model,desc) SELECT product.id_prod,prod_lang.name,product.ref from product left join product_lang on product_lang.id_prod = product.id_prod An error occurred. SQL execution error # 1065 ...

Unable to receive AJAX response for the GET request

I am attempting to achieve the following: Upon clicking on H1, jQuery is supposed to retrieve information for a specific key from the server and display it in a prompt. The PHP code resides in server.php. $link = mysql_connect("mysql.hostinger.com.ua", "_ ...

Tips for retrieving associated entities from a WCF DataService

I have developed a WCF dataservice class to send query results to a JavaScript client. Below is the pseudocode snippet of my dataservice: public class MyDataService : DataService<MyEntities> { public static void InitializeService(DataServiceConf ...

Creating a Vue Directive in the form of an ES6 class: A step-by-step

Is it possible to make a Vue directive as an ES6 Class? I have been attempting to do so, but it doesn't seem to be working correctly. Here is my code snippet: import { DirectiveOptions } from 'vue'; interface WfmCarriageDirectiveModel { ...

Determine the higher value in each column when comparing two tables

In my database, I have two tables named STUDENT and COLLEGE The data in the STUDENT table is as follows: Student_id | GPA | backlog | internship stu_a1 | 6.72 | 1 | 1 Below are the minimum requirements of each college listed in ...

Exploring Rxjs through fundamental queries

Currently, I am working on a small application using Angular 2 and have installed Rxjs 5. However, I have encountered various ways of importing the Rxjs library in tutorials. The code mentioned in the Angular 2 documentation is not functioning properly i ...

Restricting or postponing HTTP requests in an AngularJS service

I recently developed a service for my AngularJS app that retrieves data by making $http calls through various methods. However, I encountered an issue where these requests were being triggered every time an HTML element in the view (a product details div) ...

Concealing overflow in a sticky container when a fixed child element is present

I am currently working on a website where I have implemented slide-up section panels using position: sticky while scrolling. However, I am encountering issues with fixed elements within the sticky panels not properly hiding outside of the respective sectio ...

Direct users from one path to another in Express framework

I have two main routes set up in nodejs. First is the users.js route: router.post('/users/login', function(request, response) { // Logic for user login // Redirect to dashboard in dashboard.js file after login response.redirect(&ap ...

Utilizing Ring-JSON library to encode Joda DateTime in Clojure

Looking at this snippet: ; src/webapp/core.clj (ns webapp.core (:require [compojure.core :refer [defroutes GET]] [ring.middleware.json :as mid-json] [clj-time.jdbc])) (defn foo [request] {:body {:now (org.joda.time.DateTime/no ...

The Google Maps API swipe feature on mobile devices is causing issues with screen scrolling

When users visit my website on mobile, they encounter a situation where Google Maps suddenly covers the entire page, preventing them from scrolling away. This is because swiping up or down only moves the map view within Google Maps, instead of scrolling th ...

Sending information from React JS to MongoDB

I am currently facing a challenge in sending data from the front-end (react js) to the back-end (node js), and then to a mongodb database for storage. While I have successfully called the server with the data, I am encountering an issue when attempting to ...

The parent node is returning a child element

Currently working on a website using Vue 3.0 and Element+ and encountering an inconsistency. There is a div displayed on the page (an array of objects) and the goal is to remove the object from the array when a button is clicked. The issue arises when som ...

Bootstrap 4 tabs function perfectly in pairs, but encounter issues when there are three of them

Having trouble with bootstrap4 tabs not working properly? They function well with 2 tabs using the code below: <div class="row"> <div class="col-12"> <ul class="nav nav-tabs" id="registration-picker-acc-select" role="tablist"> ...

The ultimate guide to removing a targeted form input using jquery

My HTML form contains two text inputs: <form id="insert_amount",onsubmit="submitAmount();return false;"> **input(type="text",placeholder="Your message",name="message")** **input(type="text",placeholder="Your amount",name="amount") ...

The Vuetify expansion panel seems to be failing to reflect changes in the data

I've implemented pagination to display 5 expansion panels at a time from a list of over 60 items. However, I'm encountering an issue where the expansion panels are not updating correctly with the new lists. I am aware that a new list is being ret ...

What is the mechanism behind JSON.parse()?

As I delve into the realm of using JavaScript to parse an object retrieved from a PHP file, a snippet of pertinent code catches my attention: var name = document.getElementBId("name").value; var XHR = new XMLHttpRequest(); XHR.open("GET', 'looku ...

The functionality of the Node.js/Express.js app is limited to operating solely on Port 3000

I'm currently troubleshooting an issue with my Node.js/Express.js app running on my server. It seems to only work on port 3000, and I'm trying to understand why. Here's what I've discovered: When I don't specify a port (app.liste ...