I have an array of object that have different values like
items=[{id:1,category:"cat_1" , title:"My title 1"},{id:2,category:"cat_2" , title:"My title 2"},{id:6,category:"cat_1" , title:"Another title 1"},{id:1,category:"cat_3" , title:"My title 3"},{id:8,category:"cat_1" , title:"Third Title"},{id:2,category:"cat_2" , title:"Another title 2 "}]
I use array map to list the object and display them as
{
items.map((item) => (
<h1>{item.category}</h1>
<p>{item.title}</p>
))}
My question is how do i iterate the item so as it groups the items by category as follows
cat_1
- My title 1
- Another title 1
- My title 3
cat_2
- My title 2
- Another title 2
cat_3
-Third Title
reduce
is a general iteration operation, everything you can do with aFOREACH
loop (for
/in
,for
/of
,Array.prototype.forEach
), you can do withreduce
! Once you havereduce
, you no longer needmap
,filter
,groupBy
,sort
, etc. (except for readability and clarity, of course). There is a simple sketch of a proof on the Wikipedia page forfold
. So,reduce
is not just "super powerful", it is in fact "all-powerful". – Towards