SHARE
TWEET

Untitled

a guest Jan 18th, 2020 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. let input = [[1, 2, [3, 4]], function(a) { return a; }, { "name": "Martin" }, 3, 4, 5, function(a) { return (a + 1)}]
  3. let arr = eval(input);  
  4. let results = 0;
  5. let intCounter = 0;
  6. let result = [];
  7. let i = 1;
  8.  
  9.  
  10. arr.forEach(element => {
  11.    
  12.     if ( Array.isArray(element)) {  
  13.             let isNested = false;
  14.    
  15.         for(let i = 0; i < element.length; i++){
  16.             // handle nested
  17.             if(Array.isArray(element[i])){
  18.                 isNested = true;
  19.                 break;
  20.             }  
  21.         }
  22.         if (isNested) {
  23.             var newElement = element.reduce(function(prev, curr) {
  24.                 console.log(prev);
  25.                     return prev.concat(curr);
  26.                 });
  27.             results.push(newElement);
  28.         } else {
  29.             results.push(newElement);
  30.         }
  31.            
  32.     } else if ( typeof element == "object") {
  33.          
  34.         let key = Object.keys(element)[0];      
  35.         result.push(key + ": " + element[key]);
  36.        
  37.     } else if (typeof element == "number") {
  38.        
  39.         intCounter++;
  40.  
  41.     } else if (typeof element == "string") {
  42.        
  43.         //reverse string
  44.         result.push(element.split("").reverse().join(""));
  45.        // console.log( element.split("").reverse().join(""));
  46.  
  47.  
  48.     } else if (typeof element == "function") {
  49.        
  50.         //call function
  51.        
  52.         result.push(element(42));
  53.        
  54.  
  55.     }
  56. });
  57.  
  58. if(intCounter > 0) {
  59.    result.unshift(intCounter);
  60. }
  61. result =JSON.stringify(result);
  62.  
  63. console.log(result);
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top