Retrieve the latest entry from a JSON file containing epoch timestamps

Currently, I am in the process of developing an app using angularjs. In my json data, I have timestamps and corresponding values like below:

{
    "dps": {
      "1455719820": 0,
      "1455720150": 0,
      "1455720480": 0,
      "1455720810": 0,
      "1455721140": 0,
      "1455721470": 0,
      "1455721800": 0,
      "1455722130": 0
    }
  }

My dilemma lies in figuring out how to extract the value associated with the most recent timestamp from this json data structure. Any advice or insights would be greatly appreciated.

Answer №1

A method that I find highly compatible and easy to understand is as follows:

  1. Start by using the Object.keys
  2. If necessary, you can proceed to sort() them (although in this particular example, sorting isn't needed)
  3. Retrieve the value (which is always 0 in this case) by either using pop or arr[arr.length-1]

var o = {
    "dps": {
      "1455719820": 10,
      "1455720150": 90,
      "1455720480": 110,
      "1455720810": 560,
      "1455721140": 670,
      "1455721470": 120,
      "1455721800": 9,
      "1455722130": 130
    }
  }

// Just for sorting purposes
var arr = Object.keys(o.dps).sort(); // Your keys were actually already sorted
console.log(arr);

// To obtain the latest value:
var newest = arr.pop();
var val = o.dps[newest];
console.log(newest,val)

Answer №2

 let largestTimestamp = 0;
 let dataObj = {
    "dps": {
      "1455719820": 0,
      "1455720150": 0,
      "1455720480": 0,
      "1455720810": 0,
      "1455721140": 0,
      "1455721470": 0,
      "1455721800": 0,
      "1455722130": 0
    }
  }
for(let key in dataObj.dps){
    if(largestTimestamp < key){
          largestTimestamp = key;
    }
}

// largestTimestamp now holds the biggest timestamp 

Answer №3

To achieve this task, you can utilize Array#reduce. This method allows for a single loop operation without the need to sort keys beforehand.

var data = { "dps": { "1455719820": 0, "1455720150": 0, "1455720480": 0, "1455720810": 0, "1455721140": 0, "1455721470": 0, "1455721800": 0, "1455722130": 42 } },
    result = data.dps[Object.keys(data.dps).reduce(function (r, a) {
        return r < a ? a : r;
    }, 0)];

console.log(result);

Answer №4

var obj = {
  "values": {
    "1455719820": 0,
    "1455720150": 0,
    "1455720480": 0,
    "1455720810": 0,
    "1455721140": 0,
    "1455721470": 0,
    "1455721800": 0,
    "1455722130": 0
  }
}

var maxValue = Math.max.apply(Math, Object.keys(obj.values));

console.log(maxValue);

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

The Vue3 counterpart of vNode.computedInstance

I'm currently in the process of upgrading my app from Vue2 to Vue3, but I've hit a roadblock when it comes to dealing with forms. For my form elements, such as FormInput, FormTextArea, and FormCheckbox, I have been using Single File Components ( ...

Codeigniter's Implementation of AJAX Pagination with JSON Format

How can I implement pagination using ajax and json format in CodeIgniter? I have successfully implemented pagination without ajax, but now I need to load my data using ajax and json. Can someone guide me on how to add pagination in this scenario? Below is ...

I'm having trouble with my Selenium as it doesn't seem to be able to open

Hey there, I've been working on a script to login to Gmail, but I'm having trouble with entering the password after entering the email. public static void main(String[] args) throws Exception { System.setProperty("webdriver.chrome.driver", "E:&b ...

Activate night mode in ElementPlus using CDN

Is there a way to set the theme to dark in ElementUI + Vue when using CDNs instead of npm? <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width,initial-scale=1.0" /> ...

Retrieving various pieces of data in Express

After scouring through various websites, I am still unclear on how to extract multiple GET variables using Express. My goal is to send a request to a Node.JS Express server by pinging the URL with: file_get_contents('http://127.0.0.1:5012/variable1/v ...

Is it a good idea to utilize triggers when bootstrapping an application?

Currently, I am in the process of developing an internal web application for managing building parts. The main focus is on a table containing projects that are interconnected with other tables. Whenever a new project is created by a user, my goal is to ini ...

Guide on developing and releasing a Vuejs component on NPM

I have been diving deep into vue lately and incorporating it into all of the projects at my workplace. As a result, I have developed various components, particularly autocomplete. Although there are plenty of existing options out there, none seem to fulfil ...

Storing user data on the client side of a website is most effectively accomplished by

I am looking to incorporate the following features: I would like to personalize the user experience by greeting them with their login name on the webpage (e.g. 'Hello, Fred!'). I also want to display or hide certain content based on the user&apo ...

Tips on how to use console.log in react-simple-chatbot

I am currently working on a chatbot project and would like to add some features such as sending emails and popups. However, I am facing an issue with console logging elements in my code. Here is the snippet of my code: import React from "react"; ...

Is it possible to bind to a service variable in a controller without using $scope and utilizing the

As I delve into the world of controllerAs syntax in AngularJS, I've encountered a snag when attempting to bind a service variable. The traditional approach using `$scope.$watch` or `$scope.$on` would require injecting `$scope`, which seems counterintu ...

Discover a class located following a specific element even when they are not directly related

There is a group of elements all with the class month. Some of them also have the class cal, while only one has the class today. The goal is to locate the first element with the class cal, but only after finding the element with the class today. The chall ...

Serialization of deeply nested objects

I am facing a difficult problem with serializing a specific data structure and I need assistance. Below is an example that demonstrates the issue: File /*CConnector.cs*/ // Code for CConnector class File /*CElement.cs*/ // Code for CElement class File / ...

Performing a $lookup operation across various collections for a nested output

I have multiple collections and I've utilized the separate collection & foreign key approach. Now, I'm looking to combine these collections to create nested collections. Take a look at my collection schemas: const SurveySchema = new Schema({ _id ...

Differentiate among comparable values through placement regex

I'm currently tackling a challenge involving regex as I work on breaking down shorthand CSS code for the font property. Here is my progress thus far: var style = decl.val.match(/\s*(?:\s*(normal|italic|oblique)){1}/i); style = style ? style ...

Working with Multi-dimensional Arrays in JSON Using Mysql

What is the best way to store JSON code in a MySQL field? I am looking to store user data (with user_id as the key) including three values: name, age, and sex. 145:"name,age,sex", 148:"name,age,sex", 200:"name,age,sex" I am currently using MySQL version ...

The 'npm start' command is failing to recognize the changes made to the

I am embarking on a new node application journey, starting with the package.json below, which is quite basic. { "name": "mynewnodeventure", "version": "1.0.1", "description": "Exploring node", "main": "index.js", "start": "node server. ...

Troubleshooting: Why jQuery is Not Functioning Properly in Conjunction

Currently, I am in the process of developing a friend search feature. This function operates effectively; upon entering a name in the search bar, individual user profiles appear in separate div containers with their respective images and names. Each profil ...

Having trouble with incorporating a feature for uploading multiple images to the server

At the moment, I have a code snippet that allows me to upload one image at a time to the server. However, I am looking to enhance this functionality to be able to upload multiple images simultaneously. I am open to sending multiple requests to the server i ...

Stop files from being downloaded every time the page is visited

Within my Vue.js application, there is an animation that appears on a specific page. However, each time I visit that page, the assets for the animation are re-downloaded from scratch. Although the app does get destroyed when leaving the page, using v-show ...

When an input is disabled in "react-hook-form", it may return undefined

Within my React application, there exists a form containing various input fields. I have enclosed these fields using FormProvider imported from react-hook-form and utilized register within each field. import { useForm, FormProvider, useFormContext } from & ...