ob-map
v0.2.1
Published
Simple object to object mapping.
Downloads
7
Readme
ob-map
Simple object mapper for creating flat javascript objects.
Description
Maps an object to a flat structure. Map object keys from one key to another with the help of a mapping object. Supports mapping of a nested object to a flat structure.
Installation
npm
npm install --save ob-map
yarn
yarn add ob-map
Usage
Flat Structure
import { map } from 'ob-map';
map(
{
a: 1,
b: 2
},
{
y: 'a',
z: 'b'
}
);
// => {y: 1, z: 2}
Nested Structure
import { map } from 'ob-map';
const obj = {
a: {
b: {
c: 1
}
},
d: {
e: 2
}
};
const mapping = {
x: 'a.b.c',
y: 'd.e'
};
map(obj, mapping);
// => {x: 1, y: 2}
Bulk Map
import { bulkMap } from 'ob-map';
const arr = [
{
a: 1,
b: 2,
c: 3
},
{
a: 3,
b: 4,
c: 5
}
];
const mapping = {
x: 'a',
y: 'b',
z: 'c'
};
bulkMap(arr, mapping);
// => [{ x: 1, y: 2, z: 3 },{ x: 3, y: 4, z: 5 }]