我正在使用 angularjs 创建应用程序。我有像这样的 json:
{
"dps": {
"1455719820": 0,
"1455720150": 0,
"1455720480": 0,
"1455720810": 0,
"1455721140": 0,
"1455721470": 0,
"1455721800": 0,
"1455722130": 0
}
}
我想从 json 中获取最近时间的值。我不知道我该怎么做。
最佳答案
我认为最兼容且最容易阅读的方式:
- 使用Object.keys
- > sort()如果有必要(在你的例子中没有必要)
- 使用 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/