![Namecheap Takes Down Polyfill.io Service Following Supply Chain Attack](https://cdn.sanity.io/images/cgdhsj6q/production/6af25114feaaac7179b18127c83327568ff592d1-1024x1024.webp?w=800&fit=max&auto=format)
Security News
Namecheap Takes Down Polyfill.io Service Following Supply Chain Attack
Polyfill.io has been serving malware for months via its CDN, after the project's open source maintainer sold the service to a company based in China.
map-obj
Advanced tools
Package description
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.
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 });
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 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 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.
Readme
Map object keys and values into a new object
npm install map-obj
const mapObject = require('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] : mapObject.mapObjectSkip);
//=> {one: 1}
Type: object
Source object to copy properties from.
Type: (sourceKey, sourceValue, source) => [targetKey, targetValue, mapperOptions?] | mapObject.mapObjectSkip
Mapping function.
Type: object
Type: boolean
Default: true
Whether targetValue
should be recursed.
Requires deep: true
.
Type: object
Type: boolean
Default: false
Recurse nested objects and objects in arrays.
Type: object
Default: {}
Target object to map properties on to.
Return this value from a mapper
function to exclude the key from the new object.
const mapObject = require('map-obj');
const object = {one: 1, two: 2}
const mapper = (key, value) => value === 1 ? [key, value] : mapObject.mapObjectSkip
const result = mapObject(object, mapper);
console.log(result);
//=> {one: 1}
FAQs
Map object keys and values into a new object
The npm package map-obj receives a total of 19,218,509 weekly downloads. As such, map-obj popularity was classified as popular.
We found that map-obj demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
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.
Security News
Polyfill.io has been serving malware for months via its CDN, after the project's open source maintainer sold the service to a company based in China.
Security News
OpenSSF is warning open source maintainers to stay vigilant against reputation farming on GitHub, where users artificially inflate their status by manipulating interactions on closed issues and PRs.
Security News
A JavaScript library maintainer is under fire after merging a controversial PR to support legacy versions of Node.js.