toTreeArray.js 892 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. var setupDefaults = require('./setupDefaults')
  2. var each = require('./each')
  3. var assign = require('./assign')
  4. function unTreeList (result, array, opts) {
  5. var optChildren = opts.children
  6. var optData = opts.data
  7. var optClear = opts.clear
  8. each(array, function (item) {
  9. var children = item[optChildren]
  10. if (optData) {
  11. item = item[optData]
  12. }
  13. result.push(item)
  14. if (children && children.length) {
  15. unTreeList(result, children, opts)
  16. }
  17. if (optClear) {
  18. delete item[optChildren]
  19. }
  20. })
  21. return result
  22. }
  23. /**
  24. * 将一个树结构转成数组列表
  25. *
  26. * @param {Array} array 数组
  27. * @param {Object} options { children: 'children', data: 'data', clear: false }
  28. * @return {Array}
  29. */
  30. function toTreeArray (array, options) {
  31. return unTreeList([], array, assign({}, setupDefaults.treeOptions, options))
  32. }
  33. module.exports = toTreeArray