# map-obj

> Map object keys and values into a new object

Latest version **6.0.0** (published 2025-09-21) · MIT license · 0 weekly downloads

## Install

```sh
npm install map-obj
pnpm add map-obj
yarn add map-obj
bun add map-obj
```

## Health

**Score 60/100 (C)** — status: stable.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 6.0.0 |
| Published | 2025-09-21 |
| First published | 2014-10-12 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM |
| Node | >=20 |
| Dependencies | 0 |
| Unpacked size | 16.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 217 |
| Author | Sindre Sorhus |
| Maintainers | sindresorhus |
| Keywords | map, object, key, keys, value, values, iterate, iterator, rename, modify, deep, recurse, recursive |

## Links

- npm: https://www.npmjs.com/package/map-obj
- Repository: https://github.com/sindresorhus/map-obj
- Homepage: https://github.com/sindresorhus/map-obj#readme
- Issues: https://github.com/sindresorhus/map-obj/issues
- Funding: https://github.com/sponsors/sindresorhus
- npm.io page: https://npm.io/package/map-obj

## Recent versions

- 6.0.0 (latest) — 2025-09-21
- 5.0.2 — 2022-06-21
- 5.0.1 — 2022-02-28
- 5.0.0 — 2021-10-18
- 4.3.0 — 2021-09-19
- 4.2.1 — 2021-04-07
- 4.2.0 — 2021-03-10
- 4.1.0 — 2019-06-15
- 4.0.0 — 2019-06-07
- 3.1.0 — 2019-04-22
- 3.0.0 — 2018-08-08
- 2.0.0 — 2016-09-17
- 1.0.1 — 2015-05-02
- 1.0.0 — 2014-10-12

## README

# map-obj

> Map object keys and values into a new object

## Install

```sh
npm install map-obj
```

## Usage

```js
import mapObject, {mapObjectSkip} from 'map-obj';

// Swap keys and values
const newObject = mapObject({foo: 'bar'}, (key, value) => [value, key]);
//=> {bar: 'foo'}

// Convert keys to lowercase (shallow)
const newObject = mapObject({FOO: true, bAr: {bAz: true}}, (key, value) => [key.toLowerCase(), value]);
//=> {foo: true, bar: {bAz: true}}

// Convert keys to lowercase (deep recursion)
const newObject = mapObject({FOO: true, bAr: {bAz: true}}, (key, value) => [key.toLowerCase(), value], {deep: true});
//=> {foo: true, bar: {baz: true}}

// Filter out specific values
const newObject = mapObject({one: 1, two: 2}, (key, value) => value === 1 ? [key, value] : mapObjectSkip);
//=> {one: 1}

// Include symbol keys
const symbol = Symbol('foo');
const newObject = mapObject({bar: 'baz', [symbol]: 'qux'}, (key, value) => [key, value], {includeSymbols: true});
//=> {bar: 'baz', [Symbol(foo)]: 'qux'}
```

## API

### mapObject(source, mapper, options?)

#### source

Type: `object`

The source object to copy properties from.

#### mapper

Type: `(sourceKey, sourceValue, source) => [targetKey, targetValue, mapperOptions?] | mapObjectSkip`

A mapping function.

> [!NOTE]
> When `options.deep` is `true`, the mapper receives keys and values from nested objects and arrays. The `sourceKey` parameter is typed as `string | symbol` and `sourceValue` as `unknown` to reflect the actual runtime behavior when recursing into unknown shapes. The third argument `source` is always the original input object, not the current nested owner.

##### mapperOptions

Type: `object`

###### shouldRecurse

Type: `boolean`\
Default: `true`

Whether to recurse into `targetValue`.

Requires `deep: true`.

#### options

Type: `object`

##### deep

Type: `boolean`\
Default: `false`

Recurse nested objects and objects in arrays.

Built-in objects like `RegExp`, `Error`, `Date`, `Map`, `Set`, `WeakMap`, `WeakSet`, `Promise`, `ArrayBuffer`, `DataView`, typed arrays (Uint8Array, etc.), and `Blob` are not recursed into. Special objects like Jest matchers are also automatically excluded.

##### includeSymbols

Type: `boolean`\
Default: `false`

Include symbol keys in the iteration.

By default, symbol keys are completely ignored and not passed to the mapper function. When enabled, the mapper will also be called with symbol keys from the source object, allowing them to be transformed or included in the result. Only enumerable symbol properties are included.

##### target

Type: `object`\
Default: `{}`

The target object to map properties onto.

### mapObjectSkip

Return this value from a `mapper` function to exclude the key from the new object.

```js
import mapObject, {mapObjectSkip} from 'map-obj';

const object = {one: 1, two: 2};
const mapper = (key, value) => value === 1 ? [key, value] : mapObjectSkip;
const result = mapObject(object, mapper);

console.log(result);
//=> {one: 1}
```

## Related

- [filter-obj](https://github.com/sindresorhus/filter-obj) - Filter object keys and values into a new object

---
_Source: https://npm.io/package/map-obj · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
