-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?

9
  • 4
    You can't use key: value syntax in arrays, only objects. The second result is not valid. Commented Aug 22, 2019 at 17:34
  • 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. Commented Aug 22, 2019 at 17:38
  • 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. Commented Aug 23, 2019 at 14:36
  • 2
    This question is being discussed on Meta. Commented Aug 23, 2019 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? Commented Aug 26, 2019 at 12:29

2 Answers 2

-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}))}))
Sign up to request clarification or add additional context in comments.

Comments

-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)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.