Coffeescript for loop refactoring?

Looks like you are using Backbone. Js, which includes Underscore. Js, which has the groupBy function You could create a "bucketNumber" function: bucketNumber = (value, index) -> Math.

Floor( index / @collection. Length * 3 ) Then group your collection: sets = @collection. GroupBy bucketNumber Now, assuming ten items sets should look something like this: {0: {}, {}, {}, 1: {}, {}, {}, 2: {}, {}, {}, {}} From here, it becomes rather straight-forward for bucketNumber, bucket of sets @render new BusinessUnits( bucket ) Here is a jsFiddle showing it in action.

Looks like you are using Backbone. Js, which includes Underscore. Js, which has the groupBy function.

You could create a "bucketNumber" function: bucketNumber = (value, index) -> Math. Floor( index / @collection. Length * 3 ) Then group your collection: sets = @collection.

GroupBy bucketNumber Now, assuming ten items, sets should look something like this: {0: {}, {}, {}, 1: {}, {}, {}, 2: {}, {}, {}, {}} From here, it becomes rather straight-forward for bucketNumber, bucket of sets @render new BusinessUnits( bucket ) Here is a jsFiddle showing it in action.

That is a nice answer and what I will probably use but I am trying to understand coffeescript better. – dagda1 18 hours ago.

You don't need to keep track of your position twice, x is enough: splitCollection: => setSize = Math. Ceil @collection. Length / 3 sets = Math.

Ceil @collection. Length / maxLength for x in 1..sets @render new BusinessUnits @collection. Modelsx * setSize...(x+1) * setSize Note that there is nothing wrong with passing slice an end greater than the array length.

If I understand your code, you want to split an array in 3 parts (the last one can have less items). In this case write the reusable abstraction for the task. Using underscore: splitCollection: => group_size = Math.

Ceil(@collection.size() / 3) _. Each _(@collection. Models).

InGroupsOf(group_size), (group) -> @render(new BusinessUnits(group)) _. InGroupsOf can be written: _. Mixin({ inGroupsOf: function(array, n) { var output = ; for(var index=0; index Length; index += n) { output.

Push(array. Slice(index, index+n)); } return output; } }).

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions