5.0.2 • Published 2 years ago

map-obj v5.0.2

Weekly downloads
16,910,494
License
MIT
Repository
github
Last release
2 years ago

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}

Related

  • filter-obj - Filter object keys and values into a new object

camelcase-keysdecamelize-keysarchetype-libraryeasy-select-rnchinjowwchinjowreact-native-bluetooth2killi8n-react-native-fast-image@arisageha/react-lazyloadairscanairscan-examplebb-chatreact-native-esc-pos-sahaab@borisovart/atol-kkt-module@frxf/frxfdeneme323112@ntt_app/react-native-custom-notificationreact-native-covid-sdk@jttechnic/interpreterttiocthpb-plugin-social@olivervorasai/slidermysql-formatpanqibaoreact-native-printer-brothersreact-native-shekhar-bridge-testcogoportutils@oiti/documentoscopy-react-native@mink-opn/build-tokensquoc-testunblock-block-save-variables@saaspe/componentsexpand-react-bridgeopea-bootstraapluminos-ui-core@everything-registry/sub-chunk-2128iqra-calculatorjawwy-sdkjawwy_gamification_releasereact-native-sphereuisphereuijawwy_libraryreact-native-credit-card-pkgreact-native-jawwy_sampledecamelize-keys-deep2date-to-block-ethdemo-test-scrn@dinert/echartsdevitzer-general-cli@dotconf-pro/dotconf-pro@dotconf-pro/dotenvdfeuk-frontenddfeuk-frontend-manualdead-by-daylightdesign-system-fitbank-450decamelcase-keysdecamelize-keys-deep@dream2023/data-mappingdigital-keyboard-demosdatabase-modulefawaterak-online-paymentfawatrak-online-paymentfenglin-uploaderexternalifyexpress-snake-to-camel-case-converterfhir2file-timesfirebase-sign-in-with-ethereumfixed_form_builderfluent.adflow.reactnativesdkfluent.adflow.reactnativesdk-alphafn-lib-examplefmslfluid-tailwindfreemambafront-end-engineeringfront-corefront-and-backftl-netlify-builddiscordjs-con-selfdogandev-simple-toastdlayerdotenv-coredotex-plugindiscord.js-bycondiscordouth3emr-tabulator-tableseslint-config-featureses-react-bridgeexposifyevanutils@hbglobal/react-native-actions-shortcuts@hawkingnetwork/react-native-tab-viewhumanize-keys@hanse-kim/replace-keys@greminder/bootstrap-material-designicssify@geeky-apo/react-native-advanced-clipboardip-design@hemith/react-native-tnk
5.0.2

2 years ago

5.0.1

2 years ago

5.0.0

3 years ago

4.3.0

3 years ago

4.2.1

3 years ago

4.2.0

3 years ago

4.1.0

5 years ago

4.0.0

5 years ago

3.1.0

5 years ago

3.0.0

6 years ago

2.0.0

8 years ago

1.0.1

9 years ago

1.0.0

10 years ago