javascript - 从具有纪元时间的 json 获取最新记录

标签 javascript angularjs json

我正在使用 angularjs 创建应用程序。我有像这样的 json:

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

我想从 json 中获取最近时间的值。我不知道我该怎么做。

最佳答案

我认为最兼容且最容易阅读的方式:

  1. 使用Object.keys
  2. > sort()如果有必要(在你的例子中没有必要)
  3. 使用 pop 或 arr[arr.length-1] 访问值(在您的示例中始终为 0)

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

// just sorting
var arr = Object.keys(o.dps).sort(); // your keys were actually already sorted
console.log(arr);

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

关于javascript - 从具有纪元时间的 json 获取最新记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39219730/

相关文章:

javascript - 使用js脚本的PHP重定向无法正常工作

javascript - 如何使用带有重定向而不是新窗口的 Google 标志?

javascript - 仅在服务器 : Restful service + angularjs 上保存更改的对象

javascript - 如何使用 angularjs 将数据库中的视频链接嵌入 iframe 中

javascript - 循环访问通过 URL 链接的 API 结果页面

json - Firebase 数据库搜索查询

javascript - Node : Find object from array not contained in another array

javascript - 使用 Cocos2d-JS 在 Django 应用程序中加载 .js 文件时出错

javascript - selectboxit 的 AngularJs 问题

python - 使用 C++ 客户端通过 Zmq 发送 JSON 对象 - Python 服务器