Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

lodash.keyby

Package Overview
Dependencies
Maintainers
3
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

lodash.keyby

The lodash method `_.keyBy` exported as a module.

  • 4.6.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
3
Created

What is lodash.keyby?

The lodash.keyby package is a utility function from the Lodash library that creates an object composed of keys generated from the results of running each element of a collection through an iteratee function. The corresponding value of each key is the last element responsible for generating the key.

What are lodash.keyby's main functionalities?

Basic Usage

This feature demonstrates the basic usage of lodash.keyby where an array of objects is transformed into an object keyed by the specified property ('dir' in this case).

const _ = require('lodash.keyby');
const array = [
  { 'dir': 'left', 'code': 97 },
  { 'dir': 'right', 'code': 100 }
];
const result = _.keyBy(array, 'dir');
console.log(result); // { 'left': { 'dir': 'left', 'code': 97 }, 'right': { 'dir': 'right', 'code': 100 } }

Using a Function as Iteratee

This feature shows how to use a function as the iteratee to generate the keys. In this example, the objects are keyed by their 'type' property.

const _ = require('lodash.keyby');
const array = [
  { 'name': 'apple', 'type': 'fruit' },
  { 'name': 'carrot', 'type': 'vegetable' }
];
const result = _.keyBy(array, function(o) { return o.type; });
console.log(result); // { 'fruit': { 'name': 'apple', 'type': 'fruit' }, 'vegetable': { 'name': 'carrot', 'type': 'vegetable' } }

Handling Duplicate Keys

This feature demonstrates how lodash.keyby handles duplicate keys. The last element with the duplicate key will overwrite the previous ones.

const _ = require('lodash.keyby');
const array = [
  { 'name': 'apple', 'type': 'fruit' },
  { 'name': 'banana', 'type': 'fruit' }
];
const result = _.keyBy(array, 'type');
console.log(result); // { 'fruit': { 'name': 'banana', 'type': 'fruit' } }

Other packages similar to lodash.keyby

Keywords

FAQs

Package last updated on 13 Aug 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc