How to use the groupBy function from underscore

Find comprehensive JavaScript underscore.groupBy code examples handpicked from public code repositorys.

underscore.groupBy is a utility function that groups a given collection of items by a specified criteria, returning an object with keys representing the criteria and values being arrays of items that satisfy the criteria.

270
271
272
273
274
275
276
277
278
} else {
    qfs.listTree(postsRoot, function (name, stat) {
        return postRegex.test(name);
    }).then(function (files) {
        // Lump the posts together by day
        var groupedFiles = _.groupBy(files, function (file) {
            var parts = file.split('/');
            return new Date(parts[1], parts[2] - 1, parts[3]);
        });
fork icon55
star icon265
watch icon27

+ 38 other calls in file

154
155
156
157
158
159
160
161
162
163
164
165
166


  return invitations;
}


function _addPendingMembers(members, emails) {
  const membersByEmail = groupBy(members, member => member.email);


  for (const email of emails) {
    if (membersByEmail[email] === undefined) {
      const pendingMember = {
fork icon0
star icon0
watch icon1

How does underscore.groupBy work?

underscore.groupBy is a function that takes an array of objects and groups them based on a given criterion. The function returns an object where the keys are the distinct values of the criterion and the values are arrays of objects that match that criterion. The groupBy function loops through the input array, applies the criterion to each object, and adds each object to the corresponding array of objects in the result object.

16
17
18
19
20
21
22
23
24
25
const post = "|[NEW POST]| by " + userRecord.nickName + ":\n\n" + postRecord.post + "\n\n|[END OF POST]|\nReplies to this message will be forwarded to " + userRecord.nickName


const usersBoardRecords = await pb.collection('usersBoard').getFullList(1000000, { filter: 'boardId="' + boardId + '" && active=True', expand: 'userId' })

const usersBoardRecordsByChatChannel = _.groupBy(usersBoardRecords, (curRecord) => curRecord.expand.userId.chatChannel)

usersBoardRecordsByChatChannel.sms.forEach((curRecord) => {
    const boardPhone = curRecord.phoneUsed
    const userId = curRecord.expand.userId.id
fork icon0
star icon0
watch icon1

802
803
804
805
806
807
808
809
810
811
        stratMin: pick.withTrend.stratMin,
        tickers: pick.withTrend.map(obj => obj.ticker),
    }))
    .filter(Boolean);
const [prevSuprDwnPicks, notSuprDwnPicks] = partition(picksWithTrend, pick => pick.strategyName.includes('supr-dwn'));
const suprDwnByTicker = groupBy(prevSuprDwnPicks.map(pick => ({ ...pick, ticker: pick.withTrend[0].ticker })), 'ticker');
const DONT_RECOMMEND_IF_ALREADY_RECOMMENDED_THIS_PERCENT_BELOW = -2.25;
const suprDwnTickersToAvoid = Object.keys(suprDwnByTicker).filter(key => {
    const picks = suprDwnByTicker[key];
    return picks.some(pick => 
fork icon0
star icon0
watch icon1

+ 5 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
const people = [
  { name: "Alice", age: 25 },
  { name: "Bob", age: 30 },
  { name: "Charlie", age: 25 },
  { name: "Dave", age: 30 },
];

const groupedByAge = _.groupBy(people, "age");
console.log(groupedByAge);

This will output: css Copy code

32
33
34
35
36
37
38
39
40
41
    picks.map(pick => ({
        ...pick,
        strategyName: key
    }))
);
const grouped = groupBy(
    Object.values(withStrategies).flat(),
    'ticker'
);
const maxCalcdArray = Object.values(
fork icon0
star icon0
watch icon1

+ 5 other calls in file