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
lodash.mapkeys
lodash.mapkeys is a method from the Lodash library that allows you to create an object with the same values as the original object but with keys mapped using a provided function. It is similar to map-obj but is part of the larger Lodash utility library.
object-map
object-map is a package that provides a function to map the values of an object to new values based on a provided function. It is similar to map-obj but focuses only on mapping values, not keys.
deep-map-object
deep-map-object is a package that allows deep mapping of object keys and values. It is similar to map-obj's deep mapping feature but is specifically designed for deep transformations.
map-obj
Map object keys and values into a new object
Install
$ npm install --save map-obj
Usage
var mapObj = require('map-obj');
var newObject = mapObj({foo: 'bar'}, function (key, value, object) {
return [value, key];
});
License
MIT © Sindre Sorhus