-8

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?

George Stocker
  • 57,289
  • 29
  • 176
  • 237
Jimmy Boyd
  • 39
  • 2
  • 4
    You can't use `key: value` syntax in arrays, only objects. The second result is not valid. – Barmar Aug 22 '19 at 17:34
  • And you can't have duplicate keys in an object. – Barmar Aug 22 '19 at 17:35
  • Maybe you mean something like `{"orange": ["First", "Second"], "banana": ["Third", "Fourth"]}` – Barmar Aug 22 '19 at 17:36
  • 8
    Hello, welcome to Stack Overflow! You're being downvoted because you listed your problem and desired results, but you didn't ask a specific question or give an example of how you have tried to do it. Please add that information and you'll get much better answers. – Ian Aug 22 '19 at 17:38
  • 1
    Thank you Barmar, my mistake, I've just fixed it – Jimmy Boyd Aug 22 '19 at 17:42
  • 1
    Barmar & Ian, thank you for the warm welcome on my 1st post in StackOverflow with your downvotes... :-) of course I tried to solve the problem first and I recapped it agilely in my first quote by saying "using classic for loops and conditions". Not sure it would be wise to give an example with fors and ifs as it would be so ex games in my case, aiming for functional features. Anyhow thank you for letting me know these strict regulations. – Jimmy Boyd Aug 22 '19 at 19:55
  • 3
    @JimmyBoyd I agree that it doesn't make much sense in giving coding examples in a method that's *not* the method you wish to use. Showing someone you know how to build a shed out of bricks doesn't help them provide you instructions on how to build one out of wood, after all. Further, Ian's comment is simply flat out wrong -- you *did* ask a specific, answerable question. The only "real" issue with this question is that it has already been asked (in varying forms, of course, not identical ones) and answered here on Stack Overflow. That issue has since been rectified, though. – TylerH Aug 23 '19 at 14:36
  • 2
    This question is being [discussed on Meta](https://meta.stackoverflow.com/questions/388795/this-question-asking-what-a-functional-version-of-a-code-would-look-like-has-bee). – TylerH Aug 23 '19 at 14:36
  • 2
    It makes no sense to delete a duplicate; how else will new people who use this terminology find the source duplicate? – George Stocker Aug 26 '19 at 12:29

2 Answers2

-2

First one:

    let arr = [
        {type:"orange", title:"First"},
        {type:"orange", title:"Second"},
        {type:"banana", title:"Third"},
        {type:"banana", title:"Fourth"}
    ];

    let oranges = arr.reduce((obj, cur) => {
        if(cur.type === "orange") {
          obj.push(cur)
        }
        return obj

    },[])


    let bananas = arr.reduce((obj, cur) => {
        if(cur.type === "banana") {
          obj.push(cur)
        }
        return obj

    },[])

    console.log(oranges)
    console.log(bananas)
S.S. Anne
  • 15,171
  • 8
  • 38
  • 76
kam773
  • 83
  • 2
  • 10
-2

First find your keys:

const keys = [...new Set(arr.map(x=>x.type))]

And then filter the array and map the properties:

keys.map(x=>({[x]:arr.filter(s=>s.type==x).map(r=>({title:r.title}))}))
S.S. Anne
  • 15,171
  • 8
  • 38
  • 76