Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

nested array generator for js

// Random test
for (var test_num = 0; test_num <= 20; test_num++){
    var nested_array = set_nested_array(0);
    console.log('Test ' + test_num + '; nested_array = ');
    console.log(nested_array);
    console.log('Flattened array -> ' + nested_array.get_flattened_array());
}


/************************************/
/*  Create random nested arrays     */
/************************************/
function set_nested_array(level) {

    var max_elements = Math.floor((Math.random() * 8));    // Set a random length array
    var max_level = 10;                                    // Stop recursivity at 10 level
    var nested_array = [];

    for (var index = 0; index < max_elements; index++) {
        if (level <= max_level && Math.floor((Math.random() * 3)) == 0) { // 33% probability of new nested array
            nested_array.push(set_nested_array(level + 1));
        } else {
            switch (Math.floor((Math.random() * 8))) {
                case 0:	nested_array.push(null);
                case 1:	nested_array.push('string');
                case 2:	nested_array.push({ prop1: 'x' });
                default:	nested_array.push(Math.floor((Math.random() * 100)));
            }

        }
    }                
    return nested_array;                
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: jquery select convert into input text 
Javascript :: when reload the page the route gone in react js laravel 
Javascript :: isnumber javascript 
Javascript :: myModal 
Javascript :: npm i react-use-navigator-permissions 
Javascript :: javascript check if input is empty 
Javascript :: how to get query data using javascript 
Javascript :: Confirm the EndingPassed--Javascript 
Javascript :: localStorage check 
Javascript :: buffer to base 64 online 
Javascript :: how to set particle js not hovering over contents 
Javascript :: Navigation sidebar animated 
Javascript :: react-tournament-ready 
Javascript :: flyweight 
Javascript :: Search products by includes in javascript 
Javascript :: discord.js dm 
Javascript :: option 1 
Javascript :: empty donut chart chart js 
Javascript :: how to access res.locals in express 
Javascript :: jquery hide elevateZoom 
Javascript :: Zoho Creator Javascript Loop through more than 200 records 
Javascript :: import all var js 
Javascript :: Implicit Return Shorthand in javascript 
Javascript :: vs code javascript type check 
Javascript :: speed of sound 
Javascript :: How display console log in frontend 
Javascript :: implict type coercion in js 
Javascript :: jquery redirect to another page on radio button 
Javascript :: check stored jwt expiration 
Javascript :: route methods 
ADD CONTENT
Topic
Content
Source link
Name
3+9 =