https://github.com/niradler/object-remap
Remap object fields to a new structure.
https://github.com/niradler/object-remap
manipulation objet
Last synced: 5 months ago
JSON representation
Remap object fields to a new structure.
- Host: GitHub
- URL: https://github.com/niradler/object-remap
- Owner: niradler
- License: mit
- Created: 2019-05-11T11:37:29.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2020-02-10T23:48:29.000Z (over 5 years ago)
- Last Synced: 2024-12-06T01:00:39.141Z (6 months ago)
- Topics: manipulation, objet
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/object-remap
- Size: 18.6 KB
- Stars: 10
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# Object Remap
Remap object fields to a new object structure.
## Setup
```
npm i -S object-remap
```## Usage
```
const objectRemap = require("object-remap");const obj = {
a: 1,
b: 2,
c: 3,
d: {
e: 4,
f: [1, 2, 3]
}
};//simple
const fields = ["a", "b", "c"];let newObj = objectRemap(obj, fields);
console.log({ obj, newObj });
/*
{
obj: { a: 1, b: 2, c: 3, d: { e: 4, f: [Array] } },
newObj: { a: 1, b: 2, c: 3}
}
*///advance
const fieldsMap = [
{
origin: "a",
target: "a"
},
{
origin: "d.e",
target: "e"
},
{
origin: "d.f",
target: "f",
formatter: data => `${data.length} items`
}
];newObj = objectRemap(obj, fieldsMap);
console.log({ obj, newObj });
/*
{
obj: { a: 1, b: 2, c: 3, d: { e: 4, f: [Array] } },
newObj: { a: 1, e: 4, f: '3 items' }
}
*/
```