jQuery each method

jQuery each method

In this blog, let us know how can we iterate over array and objects in jQuery.

jQuery each is one of the utility methods to iterate over array or objects which is provided as argument.

Let us see the syntax,

$.each(, function(, {

Also we are going to see the jQuery example code for using each method to iterate over array of strings, objects and output of the program.

<!DOCTYPE html>
    <title>Page Title</title>
    <script src="https://code.jquery.com/jquery-1.9.1.min.js"></script>
    <script type="text/javascript">
        $(function () {
            var arrStr = ["Testing1", "Testing2", "Testing3"];
            var obj = {
                "key1": "value1", "key2": "value2"
            $('#result').append("<p>Iterates array</p>");
            $.each(arrStr, function (index, val) {
                $('#result').append("<p>string " + index + ": " + val + "</p>");
            $('#result').append("<p>Iterates object</p>");
            $.each(obj, function (key, val) {
                $('#result').append("<p>string " + key + ": " + val + "</p>");

       <div id="result"></div>


eacch method result

This jQuery performs following,

iterate over the array of strings and append the array element with index inside result div element.

iterate over the obejcts and append the objects field value with field name (key value pair) inside result div element.

Also prints hte difference of iterate over array and objects to understand the ddifference.

Privacy Policy  |  Copyrightcopyright symbol2020 - All Rights Reserved.  |  Contact us   |  Report website issues in Github   |  Facebook page   |  Google+ page

Email Facebook Google LinkedIn Twitter