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

Similar Reads

Using the array[index] method

Accessing JSON array values using the numerical indexes. we can directly access the JSON array by accessing their indexes....

Using the array[key] method

...

Using the array.slice method

Accessing JSON array values within the JSON array elements using specific corresponding keys....

Using for…of the loop

...

Using forEach()

Extracting JSON arrays portion of values using slice method. slice() method returns a new array containing a portion of the array on which it is implemented. The original remains unchanged....

Using the map() method

...

Using filter() method

Accessing values from the JSON array by Iterating through each element of the JSON array. for…of statement iterates over the values of an iterable object (like Array, Map, Set, arguments object, …,etc), executing statements for each value of the object....

Using the find() method

...

Using findIndex() method

Extracting values from the JSON arrays by executing a function for each element of the array. forEach() method calls the provided function once for each element of the array. The provided function may perform any kind of operation on the elements of the given array....

Using some() method

...

Using recursive traversal of JSON array

Accessing values from the JSON array by creating a new modified array from the existing JSON array values. map() method creates an array by calling a specific function on each element present in the parent array. It is a non-mutating method....

Using the custom reduce method

...

Using custom classes

Extracting the values from the JSON array by creating a new array with the filtered elements from the original existing array. filter() Method is used to create a new array from a given array consisting of only those elements from the given array that satisfy a condition set by the argument method....