Guest User

Untitled

a guest
Oct 10th, 2019
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public ngOnInit() {
  2.   const blah = [
  3.     "division",
  4.     [
  5.       "blah",
  6.       "blah1",
  7.       [
  8.         "division",
  9.         "blah",
  10.         [
  11.           "division",
  12.           "division",
  13.  
  14.         ]
  15.       ],
  16.       [
  17.         "blah",
  18.         "division",
  19.         "blah"
  20.       ]
  21.     ]
  22.   ];
  23.  
  24.   const flattened = this.reduce(blah);
  25.   console.log(flattened);
  26. }
  27.  
  28. public reduce(arr: any[]) {
  29.   let out = [];
  30.  
  31.   arr.forEach((value, index) => {
  32.     if (Array.isArray(value)) {
  33.       out = out.concat(this.reduce(value));
  34.     }
  35.  
  36.     if (value === 'division') {
  37.       out = out.concat(value);
  38.     }
  39.   });
  40.  
  41.   return out;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment