3

I am retrieving data from a football (soccer) API. The specific data I need is an array of objects (306 objects). Every object has a property called matchday with a numeric value. I want to group all the objects that share the same property and store them in an array. What I need in the end is an array of array of objects.

Example array of objects:

[
  {id: 264796, matchday: 1, …},
  {id: 264797, matchday: 1, …},
  {id: 264798, matchday: 2, …},
  {id: 264800, matchday: 2, …},
]

What I want looks like this:

[
  [{id: 264796, matchday: 1, …},{id: 264797, matchday: 1, …}],
  [{id: 264798, matchday: 2, …},{id: 264800, matchday: 2, …}],
]
1
  • @CBroe: This question is better than most I've seen here in awhile. Commented Mar 18, 2020 at 13:26

4 Answers 4

4

You can use .reduce() with Object.values() to get the desired output:

const data = [
  {id: 264796, matchday: 1}, {id: 264797, matchday: 1},
  {id: 264798, matchday: 2}, {id: 264800, matchday: 2}
];

const result = Object.values(
  data.reduce((r, c) => {
    r[c.matchday] = r[c.matchday] || [];
    r[c.matchday].push(c);
    return r;
  }, {})
);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Mohammad!
1

we can use reduce

const arr = [
  {id: 264796, matchday: 1},
  {id: 264797, matchday: 1},
  {id: 264798, matchday: 2},
  {id: 264800, matchday: 2},
]

const result = arr.reduce((acc, item) => {

  if (!acc.find(accSubArr => accSubArr.find(accSubArrItem => accSubArrItem.matchday === item.matchday))) {
    acc.push(arr.filter(arrItem => arrItem.matchday === item.matchday))
  }
  return acc;
}, [])

console.log(result)

Comments

1

You can try this:

const data = [{
    id: 264796,
    matchday: 1
  },
  {
    id: 264797,
    matchday: 1
  },
  {
    id: 264798,
    matchday: 2
  },
  {
    id: 264800,
    matchday: 2
  },
]

const group = data
  .map(d => d.matchday)
  .filter((v, i, c) => c.indexOf(v) === i)
  .map(i => data.filter(d => d.matchday === i))

console.log(group)

Comments

1

To add to the existing answers, here's another way making use of Map with a predicate to determine the group-by value:

const groupBy = predicate => items => 
     Array.from(items.reduce((agg, next) => {
        const key = predicate(next);
        return agg.set(key, [].concat(agg.get(key) || []).concat(next));
    }, new Map()).values());


const data = [
{name: 'A', key: 1},
{name: 'B', key: 1},
{name: 'C', key: 2},
{name: 'D', key: 2},
{name: 'E', key: 3}
];

const grouped = groupBy(x => x.key)(data);

For the fun of it, here's a recursive version of groupBy:

const groupBy = predicate => function group([next, ...items], grouped = new Map()) {
    if (!next) {
        return Array.from(grouped.values())
    }

    const key = predicate(next);
    return group(items, grouped.set(key, [...(grouped.get(key) || []), next]));
}

And what the heck, here's a more imperative approach to add to the mix:

const groupBy = predicate => items => {
    const cache = {};

    for(item of items) {
        const key = predicate(item);
        cache[key] = [].concat(cache[key]).concat(item).filter(x => x)
    }

    return Object.values(cache);
}

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.