The main objective is to filter duplicate items from an array by any given property. The solution I'm trying to use is in js @ https://stackoverflow.com/a/31194441/618220
I tried to implement it in coffeescript. It is all good, except the scoping of the functions. I don't want _indexOfProperty function to be called from outside - since it is useless in all other contexts. But if I make it private (by removing @ in declaration), I cannot call it from within inputArray.reduce
My coffee code looks like this:
Utils = ->
@filterItemsByProperty= (inputArray,property)=>
if not _.isArray inputArray
return inputArray
r = inputArray.reduce(((a,b,c,d,e)=>
if @._indexOfProperty(a,b,property) < 0
a.push(b)
a
),[])
r
@_indexOfProperty= (a,b,prop) ->
i = 0
while i< a.length
if a[i][prop] == b[prop]
return i
i++
-1
return
window.utils = Utils
here's how I invoke it from other places:
App.utils.filterItemsByProperty(personArray,"name")
And right now, anyone can do this as well:
App.utils._indexOfProperty(1,2,3)
How to modify the coffee to stop this ?