jquery - Run recursive function on Javascript array -


i have simple javascript object:

options[0] = {     title: 'title 1',     items: ['a', 'b', 'c'] };  options[1] = {     title: 'title 2',     items: ['1', '2', '3'] };  options[2] = {     title: 'title 3',     items: ['x', 'y', 'z'] }; 

i need recursively run array on itself. output example above should 27 entries/rows:

a / 1 / x / 1 / y / 1 / z / 2 / x / 2 / y / 2 / z / 3 / x / 3 / y / 3 / z b / 1 / x b / 1 / y b / 1 / z b / 2 / x b / 2 / y b / 2 / z b / 3 / x b / 3 / y b / 3 / z c / 1 / x c / 1 / y c / 1 / z c / 2 / x c / 2 / y c / 2 / z c / 3 / x c / 3 / y c / 3 / z 

here's i've tried far:

fn_options(options);  function fn_options(options) {   $.each(options, function( index, option ) {         $.each(option.items, function( i, item ) {       console.log(item);     });   }); } 

basic fiddle here: http://jsfiddle.net/6d89f/1/

this works:

printoption(options,0,"");   function printoption(options, i, string) {     if(i>=options.length)     {         console.log(string);         return;     }      for(var j=0 ; j<options[i].items.length ; j++)     {         // console.log(options[i].items[j]);         separator = string.length>0?" / ":"";         printoption(options, i+1, string + separator + options[i].items[j]);     } } 

demo


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -