flatten.js 553 B

12345678910111213141516171819202122232425
  1. var isArray = require('./isArray')
  2. var arrayEach = require('./arrayEach')
  3. function flattenDeep (array, deep) {
  4. var result = []
  5. arrayEach(array, function (vals) {
  6. result = result.concat(isArray(vals) ? (deep ? flattenDeep(vals, deep) : vals) : [vals])
  7. })
  8. return result
  9. }
  10. /**
  11. * 将一个多维数组铺平
  12. * @param {Array} array 数组
  13. * @param {Boolean} deep 是否深层
  14. * @return {Array}
  15. */
  16. function flatten (array, deep) {
  17. if (isArray(array)) {
  18. return flattenDeep(array, deep)
  19. }
  20. return []
  21. }
  22. module.exports = flatten