Socket
Socket
Sign inDemoInstall

map-obj

Package Overview
Dependencies
0
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    map-obj

Map object keys and values into a new object


Version published
Weekly downloads
25M
increased by0.66%
Maintainers
1
Install size
9.18 kB
Created
Weekly downloads
 

Package description

What is map-obj?

The map-obj package is a utility that allows you to map object keys and values into a new object. It provides a simple way to transform objects based on a mapping function.

What are map-obj's main functionalities?

Mapping keys and values

You can map the keys and values of an object to new keys and values. For example, you can take an object with keys 'foo' and 'bar' and transform them into 'newFoo' and 'newBar' with corresponding values.

{"newKey": "newValue"}

Custom mapping function

The package allows you to provide a custom mapping function that receives the key and value of each property and returns a new key-value pair array. This function is applied to each property in the original object.

const mapObj = require('map-obj');
const newObject = mapObj({ foo: 'bar' }, (key, value) => [key.toUpperCase(), value.repeat(3)]);

Deep mapping

map-obj supports deep mapping, which means you can apply the mapping function recursively to nested objects.

const mapObj = require('map-obj');
const newObject = mapObj({ foo: { bar: 'baz' } }, (key, value) => [key, value], { deep: true });

Target option

You can specify a target object to which the mapped properties will be assigned. This allows you to merge the new properties with an existing object.

const mapObj = require('map-obj');
const target = { existing: 'prop' };
const newObject = mapObj({ foo: 'bar' }, (key, value) => [key, value], { target });

Other packages similar to map-obj

Readme

Source

map-obj

Map object keys and values into a new object

Install

npm install map-obj

Usage

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

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

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

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

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

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.

mapperOptions

Type: object

shouldRecurse

Type: boolean
Default: true

Whether targetValue should be recursed.

Requires deep: true.

options

Type: object

deep

Type: boolean
Default: false

Recurse nested objects and objects in arrays.

target

Type: object
Default: {}

The target object to map properties on to.

mapObjectSkip

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

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}
  • filter-obj - Filter object keys and values into a new object

Get professional support for this package with a Tidelift subscription
Tidelift helps make open source sustainable for maintainers while giving companies
assurances about security, maintenance, and licensing for their dependencies.

Keywords

FAQs

Last updated on 21 Jun 2022

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc