index.js
function(tree, options) {
  let list = [];
  const stack = [];
  console.log(tree);
  const _tree = _.cloneDeep(tree);
  const settings = {
    initNode: options.initNode || (node => node),
    itemsKey: options.itemsKey || 'children',
    idKey: options.idKey || 'id',
    uniqueIdStart: options.uniqueIdStart || 1,
    generateUniqueId: options.generateUniqueId ||
      (() => settings.uniqueIdStart++),
  };

  if (Array.isArray(_tree) && _tree.length) {
    // Object Array
    for (let i = 0, len = _tree.length; i < len; i++) {
      stack.push(
        flattenNodeGenerator(
          _tree[i],
          'root', // placeholder
          i,
          settings,
          stack
        )
      );
    }
  } else {
    // One object tree
    stack.push(flattenNodeGenerator(_tree, 'root', 0, settings, stack));
  }

  while (stack.length) {
    list = stack.shift()(list);
  }

  return list;
}
function(tree, options) {
  let list = [];
  const stack = [];
  console.log(tree);
  const _tree = _.cloneDeep(tree);
  const settings = {
    initNode: options.initNode || (node => node),
    itemsKey: options.itemsKey || 'children',
    idKey: options.idKey || 'id',
    uniqueIdStart: options.uniqueIdStart || 1,
    generateUniqueId: options.generateUniqueId ||
      (() => settings.uniqueIdStart++),
  };

  if (Array.isArray(_tree) && _tree.length) {
    // Object Array
    for (let i = 0, len = _tree.length; i < len; i++) {
      stack.push(
        flattenNodeGenerator(
          _tree[i],
          'root', // placeholder
          i,
          settings,
          stack
        )
      );
    }
  } else {
    // One object tree
    stack.push(flattenNodeGenerator(_tree, 'root', 0, settings, stack));
  }

  while (stack.length) {
    list = stack.shift()(list);
  }

  return list;
}
function ( tree , options ) { let list = [ ] ; const stack = [ ] ; console . log ( tree ) ; const _tree = _ . cloneDeep ( tree ) ; const settings = { initNode : options . initNode || ( node => node ) , itemsKey : options . itemsKey || 'children' , idKey : options . idKey || 'id' , uniqueIdStart : options . uniqueIdStart || 1 , generateUniqueId : options . generateUniqueId || ( ( ) => settings . uniqueIdStart ++ ) , } ; if ( Array . isArray ( _tree ) && _tree . length ) { // Object Array for ( let i = 0 , len = _tree . length ; i < len ; i ++ ) { stack . push ( flattenNodeGenerator ( _tree [ i ] , 'root' , // placeholder i , settings , stack ) ) ; } } else { // One object tree stack . push ( flattenNodeGenerator ( _tree , 'root' , 0 , settings , stack ) ) ; } while ( stack . length ) { list = stack . shift ( ) ( list ) ; } return list ; }
Flatten Object tree

@param {Array|Object} tree - Object tree
@param {Object} options - Config options
@param {Function=} [options.initNode=(node=>node)] - Initialize node
@param {String=} [options.itemsKey='children'] - Specifies item's children itemsKey
@param {String=} [options.idKey='id'] - Specifies item's idKey
@param {Number=} [options.uniqueIdStart=1] - Unique id start
@param {Function=} [options.generateUniqueId=(() => settings.uniqueIdStart++)] - Unique id generator

@return {Object[]} Flatten collection
Flatten Object tree