我想在JSON数组上使用each()
,但我的警报功能未运行。
JS
var calduration = function(){
$.each(videoinfo, function(i, obj) {
alert(obj.startdatetime);
});
};
var videoinfo = [{"startdatetime":"2014-12-21 00:23:14","totalsecondrun":"2019402310","videolist":
[{"videoid":"uoSDF234","second":"192312"},{"videoid":"0apq3ss","second":"180"}]}];
答案 0 :(得分:5)
您需要致电calduration()
。
var videoinfo = [{"startdatetime":"2014-12-21 00:23:14","totalsecondrun":"2019402310","videolist": [{"videoid":"uoSDF234","second":"192312"},{"videoid":"0apq3ss","second":"180"}]}];
var calduration = function(){
$.each(videoinfo, function(i, obj) {
alert(obj.startdatetime);
});
};
calduration();
赞this。
循环遍历videolist
数组。你可以添加一个内循环:
$.each(videoinfo, function(i, obj) {
$.each(obj.videolist, function(i, obj2) {
alert(obj2.videoid);
});
});
赞this。
答案 1 :(得分:1)
在videoinfo
函数上方添加each
,calduration
功能正常,因此您需要使用()
或调用函数进行自我调用,请参阅下面的示例代码
var videoinfo = [{"startdatetime":"2014-12-21 00:23:14","totalsecondrun":"2019402310","videolist":
[{"videoid":"uoSDF234","second":"192312"},{"videoid":"0apq3ss","second":"180"}]}];
var calduration = function(){
$.each(videoinfo, function(i, obj) {
alert(obj.startdatetime);
});
}();
或强>
calduration();
或强>
calduration.call();
<强>更新强>
$.each(videoinfo[0].videolist, function(i, obj) {
alert(obj.startdatetime);
});
或者如果videoinfo
有多个对象,那么您可以使用each()
函数迭代两次
var videoinfo = [{
"startdatetime": "2014-12-21 00:23:14",
"totalsecondrun": "2019402310",
"videolist": [{
"videoid": "uoSDF234",
"second": "192312"
}, {
"videoid": "0apq3ss",
"second": "180"
}]
}];
$.each(videoinfo, function(i, obj) {
$.each(obj.videolist, function(j, o) {
alert(o.videoid);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
答案 2 :(得分:0)
试试这个:
var videoinfo = [{"startdatetime":"2014-12-21 00:23:14","totalsecondrun":"2019402310","videolist":
[{"videoid":"uoSDF234","second":"192312"},{"videoid":"0apq3ss","second":"180"}]}];
$(document).ready(function() {
$.each(videoinfo, function(i, obj) {
alert(obj.startdatetime);
});
});