简体   繁体   中英

Does Coffeescript support nested list comprehensions?

For example, given the following structure

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

How can I express the folling (python-like) comprehension?

v.myvar for p in pages for v in p

AFAIK, you can not, see: https://github.com/jashkenas/coffee-script/issues/1191

A workaround in the meantime (until CoffeeScript gets improved):

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

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

console.log result

which outputs:

[ 1, 2, 3 ]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM