Edited at

for文でのパターンマッチ

More than 5 years have passed since last update.

これできるかなぁと思って書いたら出来たのでびっくり

ary = [[1, 2], [3, 4], [5, 6]]

for [a, b] in ary
console.log a + b

ary2 = [
{x: 1, y: 2}, {x: 3, y: 4}, {x: 5, y: 6}
]
for {x, y} in ary2
console.log x + y

obj = {
a: [1, 2], b: [3, 4], c: [5, 6]
}
for _, [a, b] of obj
console.log a + b