I can achieve splitting an object to arrays and grouped by attributes using classic for loops and conditions, but I would like to take advantage of JavaScript functional features such as map, filter and reduce. Here is what I am trying to achieve:
Here is my initial dataset:
let arr = [
{type:"orange", title:"First"},
{type:"orange", title:"Second"},
{type:"banana", title:"Third"},
{type:"banana", title:"Fourth"}
];
1st desired results would be:
[{type:"orange", title:"First"},
{type:"orange", title:"Second"}]
[{type:"banana", title:"Third"},
{type:"banana", title:"Fourth"}]
2nd desired results would be one object with two arrays:
{ "orange" : [ {type:"orange", title:"First"},
{type:"orange", title:"Second"}],
"banana" : [ {type:"banana", title:"Third"},
{type:"banana", title:"Fourth"}]
}
How do I transform the objects into an arrays that are keyed on a particular property in JavaScript?