map-obj
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|
"map-obj" is a versatile JavaScript package that allows developers to map object keys and values into a new object. It is particularly useful in situations where manipulation or transformation of object properties is needed, with the added benefit of functionalities such as recursing nested objects and excluding certain keys from the new object.
To utilize "map-obj", firstly, we would need to install it using npm command: npm install map-obj
.
Once installed, it can be imported and used in your JavaScript file with following coding pattern:
import mapObject, {mapObjectSkip} from 'map-obj';
const newObject = mapObject({foo: 'bar'}, (key, value) => [value, key]);
// This will output: {bar: 'foo'}
In this example, foo
is the key and bar
is the value of the source object. The function (key, value) => [value, key]
swaps the key-value pairs around, producing a new object {bar: 'foo'}
.
It can also be used with options such as deep mapping for nested object:
const newObject = mapObject({FOO: true, bAr: {bAz: true}}, (key, value) => [key.toLowerCase(), value], {deep: true});
// This will output: {foo: true, bar: {baz: true}}
In cases where you want to exclude certain keys from the new object, mapObjectSkip
can be utilized:
const newObject = mapObject({one: 1, two: 2}, (key, value) => value === 1 ? [key, value] : mapObjectSkip);
// This will output: {one: 1}
The extensive documentation for the "map-obj" JavaScript package can be located on the GitHub repo: map-obj repository on GitHub. The GitHub repository holds information about API usage, available functionality, detailed description for every parameter and option along with usage examples.
The readme file from GitHub, which is presented at the start of the question, gives comprehensive usage guidance and provides examples of using key features such as "deep" transformation and exclusion with the mapObjectSkip
return value. These examples should provide a handy reference for beginners and experts alike.