Security News
Node.js EOL Versions CVE Dubbed the "Worst CVE of the Year" by Security Experts
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
@antoniovdlc/map
Advanced tools
Custom map functions for arrays.
This package is distributed via npm:
npm install @antoniovdlc/map
Mapping over arrays is a common operation in JavaScript, so this library provides some common custom map functions to have a more declarative way of mapping over arrays.
You can use this library either as an ES module or a CommonJS package:
import { select, pick } from "@antoniovdlc/map";
- or -
const { select, pick } = require("@antoniovdlc/map");
The select
map function picks a single value based on a key path in an arry of objects:
import { select } from "@antoniovdlc/map";
const arr = [
{ name: "Bob", age: 23 },
{ name: "Alice", age: 32 },
{ name: "Tom", age: 60 },
{ name: "Candice", age: 45 },
];
arr.map(select("age")); // [23, 32, 60, 45]
If in some objects, the key path doesn't exist, the returned value for that index will be null
:
import { select } from "@antoniovdlc/map";
const arr = [
{ name: "Bob", age: 23 },
{ name: "Alice", age: 32 },
{ name: "Tom", age: 60 },
{ name: "Tim" },
{ name: "Candice", age: 45 },
];
arr.map(select("age")); // [23, 32, 60, null, 45]
The pick
map function is similar to the select
one, but it allows for picking multiple key paths in an array of objects:
const arr = [
{ name: { first: "Bob", last: "Garcia" }, age: 23 },
{ name: { first: "Alice", last: "Doe" }, age: 32 },
{ name: { first: "Tom", last: "Dupont" }, age: 60 },
{ name: { first: "Candice", last: "Smith" }, age: 45 },
];
arr.map(pick("name.first", "age"));
/*
[
{ name: { first: "Bob" }, age: 23 },
{ name: { first: "Alice" }, age: 32 },
{ name: { first: "Tom" }, age: 60 },
{ name: { first: "Candice" }, age: 45 },
];
*/
The pick
map function always returns an array of objects, even if it is only passed a single key path:
const arr = [
{ name: "Bob", age: 23 },
{ name: "Alice", age: 32 },
{ name: "Tom", age: 60 },
{ name: "Candice", age: 45 },
];
arr.map(pick("age"));
// [{ age: 23 }, { age: 32 }, { age: 60 }, { age: 45 }];
Finally, it ignores key paths that don't exist in the objects:
const arr = [
{
name: { first: "Bob", last: "Garcia" },
age: { value: 23, birthday: { year: 1999, month: 3 } },
},
{
name: { first: "Alice", last: "Doe" },
age: { value: 32, date: { year: 1990, month: 1 } },
},
{
name: { last: "Dupont" },
age: { value: 60, date: { year: 1962, month: 10 } },
},
{
name: { first: "Candice", last: "Smith" },
age: { value: 45, birthday: { month: 8 } },
},
];
arr.map(pick("name.first", "age.birthday.year"));
/*
[
{ name: { first: "Bob" }, age: { birthday: { year: 1999 } } },
{ name: { first: "Alice" } },
null,
{ name: { first: "Candice" } },
]
*/
MIT
FAQs
Custom array map functions
We found that @antoniovdlc/map demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.