How to use custom classes In Javascript
Extracting values from the JSON array by creating custom classes.
Example: Using custom classes for accessing values.
Javascript
class value { constructor(jsonArray) { this .jsonArray = jsonArray; } getValue(index) { return this .jsonArray[index]; } } // Example usage const jsonArray = [{ "name" : "w3wiki" , "Location" : "Noida" }, { "courses" : [ "DSA self paced" , "DevOps Boot camp" ] } ]; const myData = new value(jsonArray); console.log(myData.getValue(1)); |
Output
{ courses: [ 'DSA self paced', 'DevOps Boot camp' ] }
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
Contact Us