0

I want to use each() on a JSON array but my alert function isn't running.

js fiddle

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

3 Answers 3

5

You need to call 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();

Like this.

To loop through the videolist array. You could add an inner loop:

$.each(videoinfo, function(i, obj) {
    $.each(obj.videolist, function(i, obj2) {
        alert(obj2.videoid);
    });
});

Like this.

Sign up to request clarification or add additional context in comments.

4 Comments

thanks noted that =) that json cannot put on bottom ?
oh one more thing! how to each videolist item ? like videoid and second
You can define videoinfo after you've declared calduration() but you must do it before calling calduration(). Personally it makes more sense to me to define it before the function it is used in.
i see, how bout another json videolist ?
1

Add videoinfo above from each function, and calduration is function so you will need to self call using () or call function, see below sample code

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);
        });
}();

OR

calduration();

OR

calduration.call();

UPDATE

$.each(videoinfo[0].videolist, function(i, obj) {
                alert(obj.startdatetime);
            });

Or if videoinfo have multiple objects then you can iterate two times using each() function

  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>

6 Comments

did u know how to each the videolist json ?
you can use videoinfo[0].videolist key in each() or if above object multiple then two iterate by each() function.. see updated answer
videolist another object that is inside parent videoinfo[1], [2]....n object.
@ImHappy see demo link in answer
hi, cannot help me one more thing ? how to select the videoid is uoSDF234 and get it second
|
0

Try this:

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);
        });
    });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.