我正在努力获得"当前"的价值。从这个JSON数据。 我需要在一行中没有任何循环。有什么建议吗?
var data=[
{"id":728,"acc":50,"date":"03-10-2017 05:45"},
{"id":727,"acc":30,"date":"03-09-2017 21:00"},
{"id":726,"acc":50,"date":"03-09-2017 05:45"},
{"id":725,"acc":30,"date":"03-08-2017 21:00"},
{"id":724,"acc":50,"date":"03-08-2017 05:45"},
{"id":723,"acc":30,"date":"03-07-2017 21:15"},
{"id":722,"acc":50,"date":"03-07-2017 05:45"},
{"id":721,"acc":30,"date":"03-06-2017 21:00"},
{"id":720,"acc":50,"date":"03-06-2017 05:45"},
{"id":719,"acc":30,"date":"03-03-2017 21:00"},
{"id":718,"acc":50,"date":"03-03-2017 05:45"},
{"id":717,"acc":30,"date":"03-02-2017 21:00"},
{"id":716,"acc":50,"date":"03-02-2017 05:45"},
{"id":715,"acc":30,"date":"03-01-2017 21:00"},
{"id":714,"acc":50,"date":"03-01-2017 05:45"},
{"id":713,"acc":30,"date":"02-28-2017 21:00"},
[
{"current":"03-10-2017 05:45"}
]
];
var mval = data[0].current;
alert(mval);
//alert(data[1].current);
谢谢!
答案 0 :(得分:0)
安全解决方案只会找到属性为current
的数组
data.find(v => v.length && v[0].current)[0].current;
答案 1 :(得分:0)
data[data.length - 1][0].current
答案 2 :(得分:0)
如果数据始终相同,则这是您的单行
var mval = data[data.length - 1][0].current;
如果数据可能发生变化,你可以循环通过数组并找到像这样的内部数组
var myVal;
for(obj in data) {
if(data[obj].constructor === Array) {
myVal = data[obj];
}
}
alert(myVal[0].current);
window.onload = function () {
var data = [
{ "id": 728, "acc": 50, "date": "03-10-2017 05:45" },
{ "id": 727, "acc": 30, "date": "03-09-2017 21:00" },
{ "id": 726, "acc": 50, "date": "03-09-2017 05:45" },
{ "id": 725, "acc": 30, "date": "03-08-2017 21:00" },
{ "id": 724, "acc": 50, "date": "03-08-2017 05:45" },
{ "id": 723, "acc": 30, "date": "03-07-2017 21:15" },
{ "id": 722, "acc": 50, "date": "03-07-2017 05:45" },
{ "id": 721, "acc": 30, "date": "03-06-2017 21:00" },
{ "id": 720, "acc": 50, "date": "03-06-2017 05:45" },
{ "id": 719, "acc": 30, "date": "03-03-2017 21:00" },
{ "id": 718, "acc": 50, "date": "03-03-2017 05:45" },
{ "id": 717, "acc": 30, "date": "03-02-2017 21:00" },
{ "id": 716, "acc": 50, "date": "03-02-2017 05:45" },
{ "id": 715, "acc": 30, "date": "03-01-2017 21:00" },
{ "id": 714, "acc": 50, "date": "03-01-2017 05:45" },
{ "id": 713, "acc": 30, "date": "02-28-2017 21:00" },
[
{ "current": "03-10-2017 05:45" }
]
];
var myVal;
for(obj in data) {
if(data[obj].constructor === Array) {
myVal = data[obj];
}
}
alert(myVal[0].current);
}
答案 3 :(得分:0)
假设数据中唯一的“数组”类型对象是包含具有“当前”键的对象的对象,您可以使用:
var current = data.filter(function(x){ return Object.prototype.toString.call( x ) === '[object Array]';} )[0][0].current;
答案 4 :(得分:0)
假设current
可以在数组中的任何位置,您需要迭代才能找到它:
var current;
data.some(function(e) {
if(e[0] && e[0].current) { //make sure it's an array with an object key "current"
current = e[0].current;
}
});
var data=[
{"id":728,"acc":50,"date":"03-10-2017 05:45"},
{"id":727,"acc":30,"date":"03-09-2017 21:00"},
{"id":726,"acc":50,"date":"03-09-2017 05:45"},
{"id":725,"acc":30,"date":"03-08-2017 21:00"},
{"id":724,"acc":50,"date":"03-08-2017 05:45"},
{"id":723,"acc":30,"date":"03-07-2017 21:15"},
{"id":722,"acc":50,"date":"03-07-2017 05:45"},
{"id":721,"acc":30,"date":"03-06-2017 21:00"},
{"id":720,"acc":50,"date":"03-06-2017 05:45"},
{"id":719,"acc":30,"date":"03-03-2017 21:00"},
{"id":718,"acc":50,"date":"03-03-2017 05:45"},
{"id":717,"acc":30,"date":"03-02-2017 21:00"},
{"id":716,"acc":50,"date":"03-02-2017 05:45"},
{"id":715,"acc":30,"date":"03-01-2017 21:00"},
{"id":714,"acc":50,"date":"03-01-2017 05:45"},
{"id":713,"acc":30,"date":"02-28-2017 21:00"},
[
{"current":"03-10-2017 05:45"}
]
];
var current;
data.some(function(e) {
if(e[0] && e[0].current) {
current = e[0].current;
}
});
console.log(current);