1. function flatten(args) {
    2. let result = [];
    3. const flattens = function(stack, result) {
    4. for(let j = 1; j < stack.length; j += 1) {
    5. if (Array.isArray(stack[j])) {
    6. flattens(stack[j], result);
    7. } else {
    8. result.push(stack[j])
    9. }
    10. }
    11. }
    12. for(let i = 0; i < args.length; i += 1) {
    13. flattens(args[i], result)
    14. }
    15. return result
    16. }

    数组扁平化

    1. function flatten(args) {
    2. let result = [];
    3. const flattens = function(stack, result) {
    4. for(let j = 1; j < stack.length; j += 1) {
    5. if (Array.isArray(stack[j])) {
    6. flattens(stack[j], result);
    7. continue
    8. }
    9. result.push(stack[j])
    10. }
    11. }
    12. for(let i = 0; i < args.length; i += 1) {
    13. if ( Array.isArray(args[i])) {
    14. flats(args[i])
    15. } else {
    16. result.push(args[i])
    17. }
    18. }
    19. return result
    20. }