All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

jQuery Each JSON Array

Last Updated : Mar 11, 2024

jQuery Each JSON Array

In this article we will show you the solution of jQuery each JSON array, here we needs to use ready() and each() methods. The ready() method triggers ready event which will occurs when the DOM has been loaded.

The each() method is designed to make DOM looping constructs concise and less error-prone and which is here used to iterates elements and runs callback each time.

Step By Step Guide On jQuery Each JSON Array :-

In script block we defined ready() method which is automatically starts loading code of within this function when this program opened on browser.

There we defined json array ‘arr’ and then using each() method we iterates json array values.

In each() method callback need to define with index, i-iteration value. Within this each key and values printed on console panel.

<!DOCTYPE html>
<html>
    <head>
        <title>Each in json array</title>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
        <script>
            $(document).ready(function(){
                var arr=[{ "Bike":"Apache",
                            "Brand":"Tvs"
                        },
                        { "Bike":"RX",
                            "Brand":"Yamaha"
                        }
                    ];
                $.each(arr, function(index,i){
                    console.log("Bike "+i.Bike);
                    console.log("Brand "+i.Brand);
                })
            })
        </script>
    </head>
</html>
  1. <!DOCTYPE html> tag which is instruct the web browser about what version of HTML file written in and it’s not have any ending tag.
  2. The<html> tag is used to indicate the beginning of HTML document.
  3. As above shown <head> tag is contain information about webpage and external file links are declared here. <title> tag is used for set the webpage title.
  4. Here we imported open source jquery library support file which is needed for coding using jquery in program.
  5. Both <head> and <title> tags having their pair end tag, so we need to close the ending tags respectively. If you’re not closed anyone of ending tag properly that is also affect the webpage result.
  6. <body> tag is beginning of main coding part because it contain coding of entire website blocks and elements described here.
  7. In script we defined ready() method within that we defined json array ‘arr’ with key, value pairs of “Bike,Apache,Brand,Tvs”,” Bike,RX,Brand,Yamaha”.
  8. Then we defined each() method with parameters, ‘arr-json array,callback function with two parameters index,iteration variable -i’.
  9. Within loop we printing each set of key, value pairs separately on console using i-iteration variable and console.log() method with respected string ‘Bike,Brand’.
  10. If you want json array with some other value just modify this and for open console right click on browser then select inspect option, at left side panel will open there choose console tab and it will displays result.
  11. Both </body>,</html> tags closed respectively. </body> tag indicates the end of body, Then </html> tag indicates the end of HTML document.

Conclusion :-

In conclusion now we are able to know how to use each method with json array in jquery.

Before execution of program we needs to confirm internet connection because then only that jquery file will supports defined jquery codes without error.

When we executes program on browser we can’t see result because we printed on console so use shortcut ‘ctrl+shift+j’ on browser console panel will open there we can see json array result.

I hope this article on jQuery each JSON array helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Dikshita

Passionate Electronics and Communication Engineering student with expertise in web development (HTML, CSS, JS,PHP, Bootstrap, React.js) and content writing. Eager problem solver and tech enthusiast, adept at creating engaging web experiences.

Follow Dikshita On Linkedin 🡪