Skip to content

Conversation

@maximilianschmitt
Copy link

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.

@marcincichocki
Copy link

Awesome, I was looking for something exactly like this. I hope this will get merged. +1

@maximilianschmitt
Copy link
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!

@kristianmandrup
Copy link

kristianmandrup commented Nov 4, 2016

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:
Would it be possible with this fix/PR?

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'
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants