How to use recursive traversal of JSON array In Javascript
Accessing the values from the JSON array by creating the custom function for seamless navigation.
Example: Using recursive traversal for accessing values.
Javascript
function JsonArrayTraversal(object) { for (let key in object) { if ( typeof object[key] === 'object' ) { JsonArrayTraversal(object[key]); } else { console.log(object[key]); } } } let jsonArray = { "name" : "w3wiki" , "location" : "Noida" , "Courses" : [ "DSA self paced course" , "DevOps Bootcamp" , "GATE preparation course" ] }; JsonArrayTraversal(jsonArray); |
Output
w3wiki Noida DSA self paced course DevOps Bootcamp GATE preparation course
How to Get a Value from a JSON Array in JavaScript ?
We are going to iterate or access the value of a JSON array in JavaScript. There are several ways and techniques to access values from the JSON Array which are explained in detail below and are further divided into several possible techniques.
Table of Content
- Using the array[index] method
- Using the array[key] method
- Using the array.slice method
- Using for…of the loop
- Using forEach()
- Using the map() method
- Using filter() method
- Using the find() method
- Using findIndex() method
- Using some() method
- Using recursive traversal of JSON array
- Using the custom reduce method
- Using custom classes