@nixat/pluck
v0.1.1
Published
A utility function to extract a property from each item in a collection
Readme
@nixat/pluck
A utility function to extract a property from each item in a collection.
Installation
# Using npm
npm install @nixat/pluck
# Using yarn
yarn add @nixat/pluck
# Using pnpm
pnpm add @nixat/pluckUsage
import { pluck, pluckDeep } from '@nixat/pluck';
// Basic usage
const users = [
{ id: 1, name: 'John' },
{ id: 2, name: 'Jane' },
{ id: 3, name: 'Bob' }
];
pluck(users, 'name'); // => ['John', 'Jane', 'Bob']
pluck(users, 'id'); // => [1, 2, 3]
// Working with nested objects
const nestedUsers = [
{ id: 1, profile: { name: 'John', age: 30 } },
{ id: 2, profile: { name: 'Jane', age: 25 } },
{ id: 3, profile: { name: 'Bob', age: 40 } }
];
// Using dot notation
pluckDeep(nestedUsers, 'profile.name'); // => ['John', 'Jane', 'Bob']
pluckDeep(nestedUsers, 'profile.age'); // => [30, 25, 40]
// Using array path
pluckDeep(nestedUsers, ['profile', 'name']); // => ['John', 'Jane', 'Bob']API
pluck(collection, key)
Extracts a property value from each item in a collection.
Parameters
collection- The array of objects to extract fromkey- The property key to extract
Returns
An array of the extracted values.
pluckDeep(collection, path)
Extracts a nested property value from each item in a collection using a path.
Parameters
collection- The array of objects to extract frompath- The property path to extract (dot notation or array)
Returns
An array of the extracted values.
License
MIT
