Reduce function from different view

The reduce () method reduces the array to a single value. The reduce() method executes a provided function for each value of the array (from left-to-right). The return value of the function is stored in an accumulator (result/total).

Reduce function from different view
Note : All the credit for this article  code used in this article goes to Mr Rajesh Pillai you can find him at Twitter: @rajeshpillai  , Medium: @rajeshpillai https://www.youtube.com/channel/UCvhIKdT611F_ebrmIbQ9VDQ

Recently one of my sir has posted a video on youtube and it made me feel that knowing the stuff is a different thing and using it in different possible ways is a different way

he has posted a video on Reduce function and how to use one

Reduce function

reduce function are basically a function on a list or array with two perameters .. well i think the image can explain better

i hope it is clear what is reduce function

so lets see it from a different view now

suppose you have an array

let data = [
  {name: "Raphel", gender: "male"},
  {name: "Tom", gender: "male"},
  {name: "Jerry", gender: "male"},
  {name: "Dorry", gender: "female"},
  {name: "Suzie", gender: "female"},
  {name: "Dianna", gender: "female"},
  {name: "Prem", gender: "male"},
];

and you want to distribute this data as

genderwise : {
"male":[
    {"name":"Raphel","gender":"male"},
    {"name":"Tom","gender":"male"},
    {"name":"Jerry","gender":"male"},
    {"name":"Prem","gender":"male"}
    ],
"female":[
    {"name":"Dorry","gender":"female"},
    {"name":"Suzie","gender":"female"},
    {"name":"Dianna","gender":"female"}
]}

so at least i would go with the more eligible answer that is for loop as

const genderwise = {male: [], female: []};
data.forEach(item => genderwise[item.gender].push(item));

so we would asl why reduce see we are not discussing the right way or wrong way the point is we are discussing the ways of using reduce function yes we can achieve the results with reduce function also and that was atleast new to me

let data = [
  {name: "Raphel", gender: "male"},
  {name: "Tom", gender: "male"},
  {name: "Jerry", gender: "male"},
  {name: "Dorry", gender: "female"},
  {name: "Suzie", gender: "female"},
  {name: "Dianna", gender: "female"},
  {name: "Prem", gender: "male"},
];


let genderwise = data.reduce((acc, currentItem, index, fullArray) => {
    acc[currentItem.gender].push(currentItem);
  
    return acc;
  
}, {male: [], female: []});

as per Rajesh sir

using reduce in this way is more functional approach for the same problem. So, rather than separating the const creation and the loop, both of them are combined into a single reduce function.  But the net output is same.  But knowing reduce will easy up lot of data processing use cases where data needs to be pipe, composed and mapped which I will share shortly, but everything can be done without reduce as well.

You can find the full video at

Thank you sir for this tips looking to see more from you