Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. flatten.js
  2. /**
  3. * Flatten an Array of Arbitrarily nested Arrays of Integers into a Flat Array of Integers.
  4. * @param {Array} intArr - Array of Numbers
  5. * @param {Array} flatArr - Flatten array of Numbers
  6. * @return {Array} - Array of Numbers
  7. */
  8. var flatten = function(integers_array, flatten_array) {
  9. // If this function is called in recursion mode, then we
  10. // need to keep previous recursion results.
  11. var all_results = flatten_array || [];
  12.  
  13. // We just want to perform any action if there's a
  14. // valid array input and this array contains any value in it.
  15. if (integers_array && integers_array.length > 0) {
  16. integers_array.forEach(function(value) {
  17. if (typeof value === 'number') {
  18. all_results.push(value);
  19. } else if (value instanceof Array) {
  20. flatten(value, all_results);
  21. }
  22. });
  23. }
  24. return all_results;
  25. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement