我正在使用angularjs.创建应用程序。我有json喜欢:
{
"dps": {
"1455719820": 0,
"1455720150": 0,
"1455720480": 0,
"1455720810": 0,
"1455721140": 0,
"1455721470": 0,
"1455721800": 0,
"1455722130": 0
}
}
我想从json获得最近时间的价值。我不知道怎么办。
答案 0 :(得分:3)
在我看来,最兼容的方式和最容易阅读:
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)
答案 1 :(得分:1)
var biggest=0;
var obj = {
"dps": {
"1455719820": 0,
"1455720150": 0,
"1455720480": 0,
"1455720810": 0,
"1455721140": 0,
"1455721470": 0,
"1455721800": 0,
"1455722130": 0
}
}
for(key in obj.dps){
if(biggest<key){
biggest = key;
}
}
// biggest will now have the biggest timestamp
答案 2 :(得分:1)
您可以使用Array#reduce
执行单个循环,而无需事先对键进行排序。
SELECT table1.id
FROM table1
LEFT JOIN table2
ON table1.id = table2.requestID
WHERE table1.username <> 'user 2'
and ( table2.username <> 'user 2' OR table2.username IS NULL )
答案 3 :(得分:1)
var o = {
"dps": {
"1455719820": 0,
"1455720150": 0,
"1455720480": 0,
"1455720810": 0,
"1455721140": 0,
"1455721470": 0,
"1455721800": 0,
"1455722130": 0
}
}
var max = Math.max.apply(Math, Object.keys(o.dps));
console.log(max);