Directory

← Back

map-map

[Node.js] Applies a callback to each key-value pair of a Map or Object.

Author

Rating

Popularity

<100

map-map

Applies a callback to each key-value pair of a Map, Object, or other collection. The original collection is not modified; a copy is returned.

Installation

Requires Node.js 7.0.0 or above.

npm i map-map

API

The module exports a single function.

Parameters

  1. Bindable: map (Array, iterator, Object, Map, Set, string, or Typed Array)
  2. mapper (function): The callback which receives three arguments (key, value, and index) and which returns a two-element array containing the new key and value.
  3. Optional: options: Object argument which is forwarded to entries-array, a dependency of this module.

Return Value

A copy of map which has had mapper applied to each of its key-value pairs.

Example

const mapMap = require('map-map')

let map = new Map([['key', 'value']])
map = mapMap(map, (key, value, index) => [key + '_mapped', value + '_mapped'])
map.get('key_mapped') // 'value_mapped'

Works on Objects too:

const mapMap = require('map-map')

let obj = {key: 'value'}
obj = mapMap(obj, (key, value, index) => [key + '_mapped', value + '_mapped'])
obj.key_mapped // 'value_mapped'

Compatibility

(Loading compatibility data...)

Was this helpful? Need more help?
Leave a comment or a question below. You can also join the chat on Discord or ask questions on StackOverflow.

Version

Dependencies

  • construct-map#^1.2.0
  • entries-array#^1.1.0
  • sbo#^1.1.0
Released
2018-11-03
Maturity
IMPORTED
License
MIT License

Compatibility

Framework
Browser
Browser Independent

map-map - Vaadin Add-on Directory

[Node.js] Applies a callback to each key-value pair of a Map or Object. map-map - Vaadin Add-on Directory
# map-map Applies a callback to each key-value pair of a Map, Object, or other collection. The original collection is not modified; a copy is returned. ## Installation Requires [Node.js](https://nodejs.org/) 7.0.0 or above. ```bash npm i map-map ``` ## API The module exports a single function. ### Parameters 1. Bindable: `map` (Array, iterator, Object, Map, Set, string, or Typed Array) 2. `mapper` (function): The callback which receives three arguments (key, value, and index) and which returns a two-element array containing the new key and value. 3. Optional: `options`: Object argument which is forwarded to [`entries-array`](https://github.com/lamansky/entries-array), a dependency of this module. ### Return Value A copy of `map` which has had `mapper` applied to each of its key-value pairs. ## Example ```javascript const mapMap = require('map-map') let map = new Map([['key', 'value']]) map = mapMap(map, (key, value, index) => [key + '_mapped', value + '_mapped']) map.get('key_mapped') // 'value_mapped' ``` Works on Objects too: ```javascript const mapMap = require('map-map') let obj = {key: 'value'} obj = mapMap(obj, (key, value, index) => [key + '_mapped', value + '_mapped']) obj.key_mapped // 'value_mapped' ```
Online