1

Consider the following code:

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'
  }
]

Is there a better way to write the nested for loops to check for product.images? without that if product.images? line?

EDIT: The accepted answer below does actually answer this question. However, I decided to change the way I am writing my code to use a custom filter instead.

In filter.js.coffee , I wrote the following:

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

The HTML goes below, notice the use of addIds in the inner 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>

And for the sake of completeness, here goes app.js.coffee

app = angular.module 'store', ['storeFilters']

app.controller 'StoreController',
  class StoreController
    constructor: ->
      @products = gems

gems = []

3 Answers 3

2

You can use when condition in a loop:

for product in @products when product.images?
  for img in product.images
    img.id = current_id
    current_id += 1
Sign up to request clarification or add additional context in comments.

1 Comment

I tried when product.images on the inner for and was wondering why it didn't work. Thanks!
0

You need to check if product.images is not null like you do now for the loop, otherwise you will have an error. The example here shows what I am talking about.

Cheers!

1 Comment

I do want to check if it's null but I want a better way of writing the code above =)
0

I'm not sure what your current id's look like (either starting from a random number or from 0 to the length of the images), but you could just grab the index within the for loop and do this:

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

or if its just the index:

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

or to be cool:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.