i want apply function each of values @ levels of array:
arr = [[1,2,3],[1,2,3],[[1,2],[1,2]],1,2,3] for example, multiply values 3, , map in same format before get:
arr = [[3,6,9],[3,6,9],[[3,6],[3,6]],3,6,9] what best way go this?
i tried use recursive function:
function mapall(array){ array.map(function(obj){ if (array.isarray(obj)===true) { return mapall(obj) } else{ return obj*3 } }) }; but when run undefined, must doing not quite right??
any ideas??
thanks
everything working, forgot return array.map. here's cleaner version of code:
var arr = [[1,2,3],[1,2,3],[[1,2],[1,2]],1,2,3]; function mapall(array){ return array.map(function(item){ return array.isarray(item) ? mapall(item) : item * 3; }); } alert(json.stringify(mapall(arr)));
Comments
Post a Comment