lodash._pickbyarray - Internal Pick By Array Function (v3)
This package exports `pickByArray`, an internal utility function from the Lodash version 3.x series, specifically `3.0.2`. It was part of a modular build strategy that offered individual Lodash functions as standalone CommonJS modules. This specific function (`_pickByArray`) was an internal helper primarily used by other Lodash methods like `_.pick` and `_.omit` when an array of property names was provided, often with an optional predicate applied to those selected keys. It is distinct from the public `_.pickBy` function, which filters an object's properties based on a predicate applied to all its own enumerable properties. The package has not been updated since the Lodash v3 era (published around January 2015). Modern Lodash (v4.x and above, with current stable version 4.18.1) integrates such functions directly into its core, and its modular `lodash-es` build offers full tree-shaking support, negating the need for these granular, unmaintained internal modules. Developers are strongly advised to use the actively maintained `lodash` or `lodash-es` package and its public APIs.
Common errors
-
TypeError: pickByArray is not a function
cause Attempting to use `pickByArray` after an incorrect import, or if the package was not installed/resolved properly. This can also occur if `import pickByArray from 'lodash._pickbyarray';` is used in a CJS context.fixEnsure the package is correctly installed (`npm install lodash._pickbyarray`) and imported using CommonJS `require`: `const pickByArray = require('lodash._pickbyarray');`. For modern usage, consider switching to `_.pickBy` from the main `lodash` package. -
SyntaxError: Cannot use import statement outside a module
cause You are attempting to use ES module `import` syntax (`import pickByArray from '...'`) with this CommonJS-only package in a script or environment that does not support ES modules or is not configured to transpile them.fixChange your import statement to `const pickByArray = require('lodash._pickbyarray');` for CommonJS compatibility. For modern ES module usage, you should instead use `import { pickBy } from 'lodash-es';` from the actively maintained Lodash ES module build. -
[TS] Module '"lodash._pickbyarray"' has no default export.
cause In a TypeScript project, the compiler is correctly identifying that this CommonJS module directly exports a function, which is not treated as a 'default export' in the ES module sense by TypeScript.fixIf you must use this legacy package, either use `const pickByArray = require('lodash._pickbyarray');` directly in your TypeScript file, or add a declaration file (`d.ts`) to inform TypeScript about its CJS export (e.g., `declare module 'lodash._pickbyarray' { function pickByArray<T>(object: T, props: string[], predicate?: (value: T[keyof T], key: keyof T, object: T) => boolean): Partial<T>; export = pickByArray; }`). However, the recommended and type-safe solution is to migrate to `import { pickBy } from 'lodash';` or `import { pickBy } from 'lodash-es';` which have official TypeScript definitions.
Warnings
- breaking This package is abandoned and has not received updates since the Lodash v3 era (published around 2015). Using it in modern applications may lead to compatibility issues with newer JavaScript features, lack of new features, and potential security vulnerabilities present in older Lodash versions that have since been patched in v4.x.
- gotcha This package exports `pickByArray`, an *internal* Lodash utility function with a specific signature (object, array of keys, optional predicate). It is distinct from the public `_.pickBy` function, which filters an object's properties based on a predicate applied to all properties. `pickByArray` is not intended for general application-level use as a direct replacement for `_.pickBy`.
- deprecated The strategy of modularizing internal Lodash functions into individual npm packages (e.g., `lodash._pickbyarray`) was primarily part of Lodash's v3 distribution. In Lodash v4 and later, these granular internal modules are no longer maintained or recommended. Modern JavaScript bundlers, when combined with `lodash-es` (the ES module build of Lodash), provide efficient tree-shaking capabilities, making the full `lodash-es` package the preferred and equally performant option for modular usage.
Install
-
npm install lodash._pickbyarray -
yarn add lodash._pickbyarray -
pnpm add lodash._pickbyarray
Imports
- pickByArray
import pickByArray from 'lodash._pickbyarray';
const pickByArray = require('lodash._pickbyarray'); - pickBy
const pickBy = require('lodash._pickbyarray');import { pickBy } from 'lodash-es'; - _
import _ from 'lodash._pickbyarray';
import _ from 'lodash'; // for ESM const _ = require('lodash'); // for CommonJS
Quickstart
// This example demonstrates the usage of the legacy lodash._pickbyarray module (v3.x).
// This internal utility was designed for specific use cases within the Lodash core
// when an array of keys needed to be processed and optionally filtered.
// For general object filtering based on a predicate, use the public _.pickBy from the main 'lodash' package (v4.x+).
const pickByArray = require('lodash._pickbyarray');
const sourceObject = {
id: 1,
name: 'Widget',
price: 9.99,
category: 'Electronics',
stock: 150
};
// Simulate an array of keys that might be selected by another internal Lodash process.
const keysToConsider = ['name', 'price', 'stock', 'nonExistentKey'];
// Use pickByArray: it typically takes the object, an array of keys to process,
// and an optional iteratee/predicate applied to values of those specific keys.
const pickedSubset = pickByArray(sourceObject, keysToConsider, (value, key) => {
// This predicate filters *among the provided keys*.
// Only pick keys where the value is a number and greater than 10.
return typeof value === 'number' && value > 10;
});
console.log('Original object:', sourceObject);
console.log('Keys considered by internal logic:', keysToConsider);
console.log('Result using legacy lodash._pickbyarray (filtered subset):', pickedSubset);
// Expected output: { stock: 150 } (as 9.99 is not > 10)
// For comparison, using the modern, public `_.pickBy` from the main Lodash library:
const _ = require('lodash'); // Using require for consistency with the example, but import { pickBy } from 'lodash-es' is preferred for ESM
const publicPickByResult = _.pickBy(sourceObject, (value) => {
// `_.pickBy` filters the entire object based on the predicate.
return typeof value === 'number' && value > 10;
});
console.log('Result using modern _.pickBy (entire object filtered):', publicPickByResult);
// Expected output: { stock: 150 }