mercredi 19 avril 2017

How to adjust flatten function to account for nested objects with an empty value in EmberJS

I have an Ember array that looks something like this: Array Image

Once flattened, it looks like this: Array Flattened

This is my function:

function flatten(parent) {
  return parent.reduce(function(sum, item) {
    var itemVal = get(item, 'value');
    if (isArray(itemVal)) {
      sum = sum.concat(flatten(itemVal));
    } else {
      sum.push(item);
    }
    return sum;
  }, []);
}

But it doesn't seem to work for this array which is 2 levels deep and has no value: First level

Second level

The root level is CONN_INFO -> CFG_SWITCH --> 412 which has no value. I would like to display the flattened array with something like this nested within Class:

key: "CONN_INFO"
value: "{CFGSwitch: {412: {}}}"




Aucun commentaire:

Enregistrer un commentaire