-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilterPropsFromObj.js
More file actions
48 lines (45 loc) · 1.16 KB
/
Copy pathfilterPropsFromObj.js
File metadata and controls
48 lines (45 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Mock object
const data = {
addition: null,
axis: 90,
baseCurve: 8.7,
color: null,
createdAt: "2020-07-04T04:41:34.1493708Z",
cylinder: -0.75,
diameter: 14.2,
distance: null,
id: "564563-3dd1-23ba-214a-1a490ff30fc7",
power: -0.5,
privateLabel: false,
productId: "190090575233",
productName: "1DBIOトーリック 90Pack",
seriesId: "ser_23432sdvdv43353dfg34",
trial: false,
upc: "190090575233",
updatedAt: null
};
// This method filters specific properties from an object.
function filterPropsFromObj(obj, keep) {
const arrProps = Object.keys(obj);
const toOmit = arrProps.filter(x => {
return !keep.includes(x);
});
let result = toOmit.reduce((total, key) => {
const { [key]: toRemove, ...toKeep } = total;
return toKeep;
}, obj);
return result;
}
// Properties from object to keep.
const keep = [
"axis",
"baseCurve",
"cylinder",
"diameter",
"power",
"productName",
"seriesId"
];
// Calling method with mock data.
console.log('[Original Object]', data) // Original Object
console.log('[Filtered Object]', filterPropsFromObj(data, keep)) // Object with filtered properties