Last active
May 5, 2018 00:59
-
-
Save bluevivid/fa12c4995c902995e9a7875cab704dd1 to your computer and use it in GitHub Desktop.
Map Diagonal
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function mapDiagonal(arr){ | |
var row_mapper = Array.from({length: arr.length}, (v, i) => i), | |
col_mapper = Array.from({length: arr[0].length}, (v, i) => i), | |
_mapper = row_mapper.map(val => col_mapper.map(_val => [val,_val])); | |
_mapper = _mapper.reduce((acc,cur)=>{ | |
acc = acc.concat(cur) | |
return acc; | |
}, []) | |
.reduce((acc,cur)=>{ | |
if(acc[cur[0] + cur[1]]){ | |
acc[cur[0] + cur[1]].push(cur) | |
} else { | |
acc[cur[0] + cur[1]] = [cur]; | |
} | |
return acc; | |
}, {}) | |
return _mapper; | |
} | |
var orig_arr = [ | |
[1,2,3,4,5], | |
[6,7,8,9,10], | |
[11,12,13,14,15] | |
]; | |
var mapped = mapDiagonal(orig_arr); | |
mapped = Object.keys(mapped).reverse().map(key => { | |
var print = mapped[key].map((val) => { | |
return orig_arr[val[0]][val[1]]; | |
}) | |
console.log(print) | |
return print; | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment