Tabnine Logo For Javascript
keyBy
Code IndexAdd Tabnine to your IDE (free)

How to use
keyBy
function
in
lodash

Best JavaScript code snippets using lodash.keyBy(Showing top 15 results out of 315)

origin: compkithq/api

const scoreLoader = () =>
 new DataLoader(async (scoreIds) => {
  const scores = await Score.find({ _id: { $in: scoreIds } }).exec()
  const scoresById = keyBy(scores, '_id')

  return scoreIds.map((scoreId) => scoresById[scoreId])
 })
origin: alferov/array-to-tree

function groupByParents(array, options) {
 var arrayByID = keyBy(array, options.customID);

 return array.reduce(function(prev, item) {
  var parentID = property.get(item, options.parentProperty);
  if (!parentID || !arrayByID.hasOwnProperty(parentID)) {
   parentID = options.rootID;
  }

  if (parentID && prev.hasOwnProperty(parentID)) {
   prev[parentID].push(item);
   return prev;
  }

  prev[parentID] = [item];
  return prev;
 }, {});
}
origin: wise-old-man/wise-old-man

/**
 * Gets the all the player deltas (gains), for every period.
 */
async function getPlayerDeltas(playerId: number) {
 const initial = await InitialValues.findOne({ where: { playerId } });
 const latest = await snapshotService.findLatest(playerId);

 const periodDeltas = await Promise.all(
  PERIODS.map(async period => {
   const deltas = await getPlayerPeriodDeltas(playerId, period, latest, initial);
   return { period, deltas };
  })
 );

 // Turn an array of deltas, into an object, using the period as a key,
 // then include only the deltas array in the final object, not the period fields
 return mapValues(keyBy(periodDeltas, 'period'), p => p.deltas);
}
origin: zetekla/react-diff-view

const mapChanges = (changes, side, toValue) => {
  if (!changes.length) {
    return [];
  }

  const computeLineNumber = side === 'old' ? computeOldLineNumber : computeNewLineNumber;
  const changesByLineNumber = keyBy(changes, computeLineNumber);
  const maxLineNumber = computeLineNumber(last(changes));
  return Array.from({length: maxLineNumber}).map((value, i) => toValue(changesByLineNumber[i + 1]));
}
origin: wise-old-man/wise-old-man

/**
 * Finds all player snapshots, grouped by period.
 *
 * Ex:
 * {
 *    day: [...],
 *    week: [...],
 *    etc
 * }
 */
async function getAllGrouped(playerId) {
 if (!playerId) {
  throw new BadRequestError(`Invalid player id.`);
 }

 const partials = await Promise.all(
  PERIODS.map(async period => {
   const list = await getAllInPeriod(playerId, period);
   return { period, snapshots: list };
  })
 );

 // Turn an array of snapshots, into an object, using the period as a key,
 // then include only the snapshots array in the final object, not the period fields
 return mapValues(keyBy(partials, 'period'), p => p.snapshots);
}
origin: wise-old-man/wise-old-man

/**
 * Given a list of groups, it will fetch the member count of each,
 * and inserts a "memberCount" field in every group object.
 */
async function attachMembersCount(groups) {
 /**
  * Will return a members count for every group, with the format:
  * [ {groupId: 35, count: "4"}, {groupId: 41, count: "31"} ]
  */
 const membersCount = await Membership.findAll({
  where: { groupId: groups.map(g => g.id) },
  attributes: ['groupId', [Sequelize.fn('COUNT', Sequelize.col('groupId')), 'count']],
  group: ['groupId']
 });

 /**
  * Convert the counts fetched above, into a key:value format:
  * { 35: 4, 41: 31 }
  */
 const countMap = mapValues(
  keyBy(
   membersCount.map((c: any) => ({ groupId: c.groupId, count: parseInt(c.toJSON().count, 10) })),
   c => c.groupId
  ),
  (c: any) => c.count
 );

 return groups.map(g => ({ ...g, memberCount: countMap[g.id] || 0 }));
}
origin: wise-old-man/wise-old-man

const memberMap = keyBy(members, 'id');
const memberIds = members.map(m => m.id);
origin: wise-old-man/wise-old-man

keyBy(
 participantCount.map((c: any) => ({
  competitionId: c.competitionId,
origin: compkithq/api

const finalsLeaderboardLoader = () =>
 new DataLoader(async (leaderboardIds) => {
  const leaderboards = await FinalsLeaderboard.find({
   _id: { $in: leaderboardIds }
  }).exec()
  const leaderboardsById = keyBy(leaderboards, '_id')

  return leaderboardIds.map(
   (leaderboardId) => leaderboardsById[leaderboardId]
  )
 })
origin: compkithq/api

const qualifiersLeaderboardLoader = () =>
 new DataLoader(async (leaderboardIds) => {
  const leaderboards = await QualifiersLeaderboard.find({
   _id: { $in: leaderboardIds }
  }).exec()
  const leaderboardsById = keyBy(leaderboards, '_id')

  return leaderboardIds.map(
   (leaderboardId) => leaderboardsById[leaderboardId]
  )
 })
origin: compkithq/api

const adminLoader = () =>
 new DataLoader(async (adminIds) => {
  const admins = await Admin.find({
   _id: { $in: adminIds }
  }).exec()
  const adminsById = keyBy(admins, '_id')

  return adminIds.map((adminId) => adminsById[adminId])
 })
origin: compkithq/api

const competitionLoader = () =>
 new DataLoader(async (competitionIds) => {
  const competitions = await Competition.find({
   _id: { $in: competitionIds }
  }).exec()
  const competitionsById = keyBy(competitions, '_id')

  return competitionIds.map(
   (competitionId) => competitionsById[competitionId]
  )
 })
origin: compkithq/api

const venueLoader = () =>
 new DataLoader(async (venueIds) => {
  const venues = await Venue.find({ _id: { $in: venueIds } }).exec()
  const venuesById = keyBy(venues, '_id')

  return venueIds.map((venueId) => venuesById[venueId])
 })
origin: compkithq/api

const workoutLoader = () =>
 new DataLoader(async (workoutIds) => {
  const workouts = await Workout.find({ _id: { $in: workoutIds } }).exec()
  const workoutsById = keyBy(workouts, '_id')

  return workoutIds.map((workoutId) => workoutsById[workoutId])
 })
origin: compkithq/api

const athleteLoader = () =>
 new DataLoader(async (athleteIds) => {
  const athletes = await Athlete.find({
   _id: { $in: athleteIds }
  }).exec()
  const athletesById = keyBy(athletes, '_id')

  return athleteIds.map((athleteId) => athletesById[athleteId])
 })
lodash(npm)keyBy

Most used lodash functions

  • LoDashStatic.map
    Creates an array of values by running each element in collection through iteratee. The iteratee is
  • LoDashStatic.isEmpty
    Checks if value is empty. A value is considered empty unless it’s an arguments object, array, string
  • LoDashStatic.forEach
    Iterates over elements of collection invoking iteratee for each element. The iteratee is invoked wit
  • LoDashStatic.find
    Iterates over elements of collection, returning the first element predicate returns truthy for.
  • LoDashStatic.pick
    Creates an object composed of the picked `object` properties.
  • LoDashStatic.get,
  • LoDashStatic.isArray,
  • LoDashStatic.filter,
  • LoDashStatic.merge,
  • LoDashStatic.isString,
  • LoDashStatic.isFunction,
  • LoDashStatic.assign,
  • LoDashStatic.extend,
  • LoDashStatic.includes,
  • LoDashStatic.keys,
  • LoDashStatic.cloneDeep,
  • LoDashStatic.uniq,
  • LoDashStatic.isObject,
  • LoDashStatic.omit

Popular in JavaScript

  • node-fetch
    A light-weight module that brings window.fetch to node.js
  • path
  • chalk
    Terminal string styling done right
  • http
  • handlebars
    Handlebars provides the power necessary to let you build semantic templates effectively with no frustration
  • mkdirp
    Recursively mkdir, like `mkdir -p`
  • fs
  • ms
    Tiny millisecond conversion utility
  • fs-extra
    fs-extra contains methods that aren't included in the vanilla Node.js fs package. Such as mkdir -p, cp -r, and rm -rf.
  • CodeWhisperer alternatives
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyTerms of usePrivacy policyJavascript Code Index
Get Tabnine for your IDE now