JSON Arrays

JSON Arrays

In the following example, we will learn how to have an array in JSON.

Example

HTML Editor
[ { "name": "Mike", "age": 36, "friends": ["John", "Antony"] }, { "name": "Alex", "age": 30, "friends": ["Jade", "Jasmine"] } ]

Mixed array

Like JavaScript, JSON can have any types of value in an array.

Example

HTML Editor
[ { "name": "Mike", "age": 36, "number": [1, "two"] }, { "name": "Alex", "age": 30, "number": ["three", 4] } ]

Looping through an Array

In the following example, we will loop through an array in JSON to filter out all the even numbers in it.

Example

HTML Editor
<!DOCTYPE html> <html lang="en-US"> <body> <h1>JSON Arrays</h1> <p id="point"></p> <button onclick="myFunction()">Click Me</button> <script> var x = document.getElementById("point"); var temp = ""; var data = [ { "name": "Mike", "age": 36, "number": [1, 12, 33, 64] } ] function myFunction(){ var myArray = data[0].number; for(var i = 0; i < myArray.length; i++){ if(myArray[i] % 2 == 0) temp += myArray[i] +" "; } x.innerHTML = temp; } </script> </body> </html>

Join Our Channel

Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.

This channel is primarily useful for Full Stack Web Developer.

Share this Page

Meet the Author