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, …}],
]
Mohammad Usman
  • 37,952
  • 20
  • 92
  • 95
Noud
  • 145
  • 9

4 Answers4

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; }
Mohammad Usman
  • 37,952
  • 20
  • 92
  • 95
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)
qiAlex
  • 4,290
  • 2
  • 19
  • 35
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)
Jonas Tomanga
  • 1,069
  • 10
  • 19
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);
}
Jamie Dixon
  • 53,019
  • 19
  • 125
  • 162