Add option to filter values that should be skipped#41
Open
maximilianschmitt wants to merge 2 commits intohughsk:masterfrom
Open
Add option to filter values that should be skipped#41maximilianschmitt wants to merge 2 commits intohughsk:masterfrom
maximilianschmitt wants to merge 2 commits intohughsk:masterfrom
Conversation
|
Awesome, I was looking for something exactly like this. I hope this will get merged. +1 |
Author
|
@hughsk have you had time to check this out? Would be really great to have this merged. Please let me know if there's anything missing. Thanks! |
|
I also need this. I would also like those not to be flattened to still be added to result object, just not with a nested key. Looking for something like this: Flatten example let obj = {
x: {
a: {
b: [{name: hello}]
c: {
name: 'hello'
status: 'done'
}
}
},
y: [],
v: 'hello'
}
//
let keepOrFlat = function(node) {
if (node.name && typeof node.name === 'string') return true
if (Array,isArray(node)) return true
if (typeof node === 'object') return false
return true
}
let flatten = function(node) {
if (typeof node !== 'object') return false
if (node.name) return false
return true
}
let keep = function(node) {
if (typeof node !== 'object') return true
if (node.name && typeof node.name === 'string') return true
}
// if keepOrFlat is set, other condition functions are ignored
let conditions = {
keepOrFlat,
flatten,
keep
}
let flatObj = obj.flatten({
sep: '.',
conditions
})
let obj = {
'x.a.b': [],
'x.a.c': {
name: 'hello',
},
y: [],
v: 'hello'
} |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
I have a use case where I'm using flat on data that has 'meta' objects as leafs. With a filter function, I could tell flat to ignore these meta-leafs, no matter how deep they're nested.