Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript get diagonals of array

function getDiagonals(grid) {
    let result = [];
    let Ylength = grid.length;
    let Xlength = grid[0].length;
    let maxLength = Math.max(Xlength, Ylength);
    let temp;
    for (let k = 0; k <= 2 * (maxLength - 1); ++k) {
        temp = [];
        for (let y = Ylength - 1; y >= 0; --y) {
            let x = k - y;
            if (x >= 0 && x < Xlength) {
                temp.push(array[y][x]);
            }
        }
        if (temp.length > 0) {
            result.push(temp);
        }
    }
  
  return result;
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: fill array with 0 javascript 
Javascript :: js random int 
Javascript :: update node ubuntu 
Javascript :: javascript remove empty elements from array 
Javascript :: js delete dot 
Javascript :: get offset of element relative to parent 
Javascript :: how to add event listener to iframe 
Javascript :: js get random data between two dates 
Javascript :: Error: Expected "payload" to be a plain object. at validate 
Javascript :: js get meta content 
Javascript :: jquery toggle class 
Javascript :: jquery create input hidden 
Javascript :: js console log without spaces 
Javascript :: check if intersectionobserver supported js 
Javascript :: javascript new date zero time 
Javascript :: javascript lerp 
Javascript :: clean way to Deal with undefined in chrome storage local get 
Javascript :: split integer into digits javascript 
Javascript :: remove extra spaces javascript 
Javascript :: does json only support ascii 
Javascript :: javascript date - 7 days 
Javascript :: how to resize react icons 
Javascript :: getelementbytagname javascript 
Javascript :: stringify 
Javascript :: convert arguments to array javascript 
Javascript :: angular 9 how to get previous state 
Javascript :: how to add elements in javascript html 
Javascript :: js check if string is base64 
Javascript :: rectbutton disable 
Javascript :: javascript format date yyyy-mm-dd 
ADD CONTENT
Topic
Content
Source link
Name
6+4 =