How to use the custom reduce method In Javascript
Accessing values from the JSON array by using the custom reduced method. reduce() method in JavaScript is used to reduce the array to a single value and executes a provided function for each value of the array (from left to right) and the return value of the function is stored in an accumulator.
Example: Using the custom reduce method for accessing values from the JSON arrays.
Javascript
let jsonArrayData = [ { "name" : "w3wiki" , "location" : "Noida" }, { "course" : [ "DSA self paced course" , "DevOps Bootcamp" , "GATE preparation course" ] } ]; function customReduceMethod(arr, func, initialValue) { let accumulator = initialValue; for (let i = 0; i < arr.length; i++) { if (arr[i].name) { accumulator = func(accumulator, arr[i].name); } } return accumulator; } let data = customReduceMethod(jsonArrayData, (acc, val) => { return acc + val; }, '' ); console.log(data); |
Output
w3wiki
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