Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
检查CoffeeScript中嵌套for循环中的数组是否为null_Coffeescript - Fatal编程技术网

检查CoffeeScript中嵌套for循环中的数组是否为null

检查CoffeeScript中嵌套for循环中的数组是否为null,coffeescript,Coffeescript,考虑以下代码: class StoreController constructor: -> @products = gems current_id = 0 for product in @products if product.images? for img in product.images img.id = current_id current_id +=

考虑以下代码:

class StoreController
    constructor: ->
      @products = gems

      current_id = 0
      for product in @products
        if product.images?
          for img in product.images
            img.id = current_id
            current_id += 1


gems = [
  {
    name: 'Dodecahedron'
    images: [
        {
            full: "assets/img0.gif"
        }
        {
            full: "assets/img1.gif"
        }
    ]
  }
  {
    name: 'Gemmy Gem'
  }
  {
    name: 'Pentagonal Gem'
  }
]
是否有更好的方法编写嵌套for循环以检查
product.images?
而不使用
if product.images?

编辑: 下面公认的答案确实回答了这个问题。然而,我决定改变我编写代码的方式,改为使用自定义过滤器

filter.js.coffee
中,我写了以下内容:

filterMod = angular.module 'storeFilters', []
current_id = 0

filterMod.filter 'addIds', ->
  return (items) ->
    # avoid $rootScope:infdig
    return items if !items? || items.processed

    for img in items
      img.id = current_id
      current_id += 1

    items.processed = true
    return items
下面是HTML,请注意内部
ng repeat

<div ng-controller="StoreController as store">
  <ul ng-repeat="product in store.products" class="list-group">
    <li class="list-group-item">
        <h3> {{product.name}} <em class="pull-right">{{product.price | currency }}</em></h3>
        <div class="gallery">
          <div ng-repeat="img in product.images | addIds ">
            {{img.id}}
            <img ng-src="{{img.full}}"/>
          </div>
        </div>
        <p>  {{product.description}}</p>
        <button ng-show="product.canPurchase">Add to Cart</button>
    </li>
  </ul>
</div>

您需要检查
product.images
是否不像现在为循环设置的那样为空,否则将出现错误。这本书展示了我所说的


干杯

当循环中的条件时,可以使用

for product in @products when product.images?
  for img in product.images
    img.id = current_id
    current_id += 1

我不确定您当前的id是什么样子(从随机数开始,或者从0到图像长度),但您可以在for循环中获取索引并执行以下操作:

for product in @products when product.images?
  img.id = current_id + i for img, i in product.images
或者如果只是索引:

for product in @products when product.images?
  img.id = i for img, i in product.images
或者说要酷一点:

img.id = i for img, i in product.images for product in @products when product.images?

我确实想检查它是否为空,但我想用一种更好的方法来编写上面的代码=)我尝试了
当product.images
位于内部时,我想知道为什么它不起作用。谢谢
img.id = i for img, i in product.images for product in @products when product.images?