I have a json, I $.each
videolist
and try to get the second
. Then, I wish to turn the second
into array[], easy for me to do next function that I want. Just not sure how to turn it into array.
var videoinfo = [{ "startdatetime": "2014-12-21 00:23:14", "totalsecondrun": "2019402310", "videolist": [{ "videoid": "uoSDF234", "second": "10" }, { "videoid": "0apq3ss", "second": "14" }] }]; var calduration = function() { var list = videoinfo[0].videolist; $.each(list, function(index, value) { alert($.makeArray(value.second)); }); }; calduration();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
I expected the output should be [10,14].
If you insist on using a $.each()
loop, as you title implies, here is one approach:
var calduration = function () {
var duration = [];
$.each(videoinfo[0].videolist, function (index, value) {
duration.push(parseInt(value.second, 10));
});
return duration;
};
I'd suggest using $.map()
, though:
var calduration = function () {
return $.map(videoinfo[0].videolist, function (value, index) {
return parseInt(value.second, 10);
});
};
..or .map()
:
return videoinfo[0].videolist.map(function (value, index) {
return parseInt(value.second, 10);
});
And if you want to use plain JS:
var calduration = function () {
return Array.prototype.map.call(videoinfo[0].videolist, function(value){
return parseInt(value.second, 10);
});
};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.