Array comprehension in JavaScript?

Viewed 1517

I was wondering what the neatest way would be to convert (from Python) a list comprehension into Javascript. Is there anything which will make this readable and not a mess?

    non_zero_in_square = [ grid[row][col]
                           for row in range(start_row, start_row+3)
                           for col in range(start_col, start_col+3)
                           if grid[row][col] is not 0
                         ]

This is quite a good example of a list comprehension, as it has multiple fors and and an if.

I should add that the range bit is covered here (I can't live without range).

3 Answers
Related