A better example of what you can do with reduce is group objects by property. Case in point, here's how to split the students by result:
students.reduce( (acc, val) => { !acc[val.result] ? acc[val.result] = [val.name] : acc[val.result].push(val.name); return acc; }, {})
Produces:
{ Fail: [ 'Kushal' ], Pass: [ 'Rahul', 'Kushal' ] }
Another interesting use case for reduce is to create a lookup table from an object and a property / key.
let phoneBook = students.reduce( (acc, val) => (acc[val.mobileNo] = val, acc), {}) > phoneBook[989481] { name: 'Kushal', class: 'MCA', result: 'Pass', mobileNo: '989481' }
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
A better example of what you can do with reduce is group objects by property. Case in point, here's how to split the students by result:
Produces:
Another interesting use case for reduce is to create a lookup table from an object and a property / key.