Last active
May 8, 2021 04:17
-
-
Save waptik/795708293e442e3f0ee0c63f40552b78 to your computer and use it in GitHub Desktop.
[SOLVED] - I need help to: 1: rename keys to lowercase values 2: remove empty objects with one non-null key
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// current code | |
const arrs = [ | |
{ | |
comments: 'asd', | |
movement: 'Back Squat', | |
userID: 'wDHZv3OL55SIymHkhMUejNleNkx1', | |
weight: '330', | |
}, | |
{ | |
comments: 'asd', | |
movement: 'Bench Press', | |
userID: 'wDHZv3OL55SIymHkhMUejNleNkx1', | |
weight: '100', | |
}, | |
{ | |
comments: 'Comment', | |
movement: 'Clean', | |
userID: 'wDHZv3OL55SIymHkhMUejNleNkx1', | |
weight: '195', | |
}, | |
{ | |
comments: 'Front squat comment alpha', | |
movement: 'Front Squat', | |
userID: 'wDHZv3OL55SIymHkhMUejNleNkx1', | |
weight: '315', | |
}, | |
{ | |
comments: 'egg', | |
movement: 'Strict', | |
userID: 'wDHZv3OL55SIymHkhMUejNleNkx1', | |
weight: '155', | |
}, | |
{ comments: 'sleep', movement: null, userID: null, weight: null }, | |
{ comments: 'look', movement: null, userID: null, weight: null }, | |
{ | |
comments: 'abc', | |
movement: 'Strict Press', | |
userID: 'wDHZv3OL55SIymHkhMUejNleNkx1', | |
weight: '155', | |
}, | |
]; | |
// expected output | |
// 1. | |
const arrs = [ | |
{ | |
comments: 'asd', | |
movement: 'Back Squat', | |
userid: 'wDHZv3OL55SIymHkhMUejNleNkx1', | |
weight: '330', | |
}, | |
{ | |
comments: 'asd', | |
movement: 'Bench Press', | |
userid: 'wDHZv3OL55SIymHkhMUejNleNkx1', | |
weight: '100', | |
}, | |
{ | |
comments: 'Comment', | |
movement: 'Clean', | |
userid: 'wDHZv3OL55SIymHkhMUejNleNkx1', | |
weight: '195', | |
}, | |
{ | |
comments: 'Front squat comment alpha', | |
movement: 'Front Squat', | |
userid: 'wDHZv3OL55SIymHkhMUejNleNkx1', | |
weight: '315', | |
}, | |
{ | |
comments: 'egg', | |
movement: 'Strict', | |
userid: 'wDHZv3OL55SIymHkhMUejNleNkx1', | |
weight: '155', | |
}, | |
{ comments: 'sleep', movement: "N/A", userid: "N/A", weight: "N/A" }, | |
{ comments: 'look', movement: "N/A", userid: "N/A", weight: "N/A" }, | |
{ | |
comments: 'abc', | |
movement: 'Strict Press', | |
userid: 'wDHZv3OL55SIymHkhMUejNleNkx1', | |
weight: '155', | |
}, | |
]; | |
// or 2. | |
const arrs = [ | |
{ | |
comments: 'asd', | |
movement: 'Back Squat', | |
userid: 'wDHZv3OL55SIymHkhMUejNleNkx1', | |
weight: '330', | |
}, | |
{ | |
comments: 'asd', | |
movement: 'Bench Press', | |
userid: 'wDHZv3OL55SIymHkhMUejNleNkx1', | |
weight: '100', | |
}, | |
{ | |
comments: 'Comment', | |
movement: 'Clean', | |
userid: 'wDHZv3OL55SIymHkhMUejNleNkx1', | |
weight: '195', | |
}, | |
{ | |
comments: 'Front squat comment alpha', | |
movement: 'Front Squat', | |
userid: 'wDHZv3OL55SIymHkhMUejNleNkx1', | |
weight: '315', | |
}, | |
{ | |
comments: 'egg', | |
movement: 'Strict', | |
userid: 'wDHZv3OL55SIymHkhMUejNleNkx1', | |
weight: '155', | |
}, | |
{ | |
comments: 'abc', | |
movement: 'Strict Press', | |
userid: 'wDHZv3OL55SIymHkhMUejNleNkx1', | |
weight: '155', | |
}, | |
]; | |
The original
filter
removes any object as long as it has a singlenull
value. In this change, there are objects that may havenull
values that we want to keep now, so we need to change our approach.Since our
filter
step is going to filter by a certain key now, I would move themap
in front of thefilter
, just so I can standardise (lowercase) the keys early. In fact, any checks againstkey
is best done after we have lowercased the keys.Hence, our approach will take 3 steps:
- Lowercase all keys, and remove the
no
key- Keep objects only if
a. it has a defined, non-null value forname
b. all values are non-null- Clean up non-null key-value pairs that remain (i.e. the object had a defined, non-null
name
property, and was not a candidate for removal)arrs .map(obj => Object.fromEntries( Object .entries(obj) .map(([k, v]) => [k.toLowerCase(), v]) .filter(([k, v]) => k !== 'no') )) .filter(obj => ( (obj['name'] !== null && obj['name'] !== undefined) || Object.values(obj).every(v => v !== null) )) .map(obj => Object.fromEntries( Object .entries(obj) .filter(([k, v]) => v !== null) ));
Thanks again! I really appreciate it.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The original
filter
removes any object as long as it has a singlenull
value. In this change, there are objects that may havenull
values that we want to keep now, so we need to change our approach.Since our
filter
step is going to filter by a certain key now, I would move themap
in front of thefilter
, just so I can standardise (lowercase) the keys early. In fact, any checks againstkey
is best done after we have lowercased the keys.Hence, our approach will take 3 steps:
no
keya. it has a defined, non-null value for
name
b. all values are non-null
name
property, and was not a candidate for removal)