3

例如,给定以下结构

    pages = [
        [
            { myvar: 1},
            { myvar: 2},
            { myvar: 3},
        ]
    ];

我如何表达以下(类似python)的理解?

v.myvar for p in pages for v in p
4

1 回答 1

6

AFAIK,你不能,见:https ://github.com/jashkenas/coffee-script/issues/1191

同时有一个解决方法(直到 CoffeeScript 得到改进):

pages = [
        [
            { myvar: 1},
            { myvar: 2},
            { myvar: 3},
        ]
    ];

result = []
for row in pages
  for map in row
    result.push map.myvar

console.log result

输出:

[ 1, 2, 3 ]
于 2012-05-10T11:05:35.707 回答