Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.
types = [
    { name: "Feature"}
    { name: "Enhancement"}
    { name: "Bug"}
    { name: "Spike"}
  ]

buildIndex = (source, property) ->
  array = []
  array[prop[property]] = prop for prop in source
  array

buildIndex types, 'name'

Result:

[Feature: Object, Enhancement: Object, Bug: Object, Spike: Object]
  Bug: Object
  Enhancement: Object
  Feature: Object
    name: "Feature" 
  Spike: Object

This is my ugly code. How to refactor this? My problem is the buildIndex function. Iyt works, but it isn't elegant.

share|improve this question

1 Answer 1

up vote 2 down vote accepted

There's something fishy here. You're creating an array, but you're not using it as one. You're simply adding named properties to it, like you can with any object. In the end, you're left with an array that still has a length of zero. So I'm betting you don't actually want an array at all.

And in that case, I'd rewrite it to this

buildIndex = (source, property) ->
  obj = {}
  obj[prop[property]] = prop for prop in source
  obj

No more array-nonsense. However, if this is indeed what you're going for, then that's pretty much it. I agree it doesn't look particularly elegant, but it's about as good as it gets with basic CoffeeScript.

However, you could use reduce, if you're targeting browsers that support it (or Node.js):

buildIndex = (source, property) ->
  source.reduce (obj, item) ->
    obj[item[property]] = item
    obj
  , {}

but as you can see, the callback-first style of reduce means you end up with a dangling comma and argument instead, which isn't terribly neat either.

Alternatively, if you're using something like underscore.js, you can use a groupBy function to do this for you

buildIndex = (source, property) ->
  _.groupBy source, (item) -> item[property]

Looks better, although it's merely hiding the same code as above.

share|improve this answer
    
Many thanks Flambino. underscore version is very elegant, but prefere use only coffescript ... replace array with object it's correct. –  WaYdotNET Feb 17 '14 at 8:17

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.