How to find all combinations of elements in JavaScript array -
i have following array:
[[a,1,x],[b,2,y],[c,3,z]]
i want able combinations of first index of each sub array , loop through combinations performing single task on each. these combinations i'm after (note need combination of same value well):
[[a,a],[a,b],[a,c],[b,a],[b,b],[b,c],[c,a],[c,b],[c,c]]
i'd loop through , each of values.
i'm not sure start here advice or pointers helpful!
you need loop through array twice. based on want can statically access first element each time:
var arr = [['a',1,'x'],['b',2,'y'],['c',3,'z']]; var newarr = []; var length = arr.length; var curr; (var = 0; < length; i++) { curr = arr[i][0]; (var j = 0; j < length; j++) { newarr.push([curr, arr[j][0]]); } } console.log(newarr);
Comments
Post a Comment