I have such input:
[ { name: 'timezone', value: 'EST' },
{ name: 'interval', value: 'day' },
{ name: 'metrics[]', value: 1},
{ name: 'metrics[]', value: 2} ]
As you may already notice - these are parameters from POST request.
What I need to do is to get such output:
[ { name: 'timezone', value: 'EST' },
{ name: 'interval', value: 'day' },
{ name: 'metrics[]', value: [ 1, 2 ] },
{ name: 'metrics[]', value: [ 1, 2 ] } ]
My code does everything it needs to do, but I'm not sure if it is written in the optimal way, am I overlooking some bugs, etc.
arrayify = (params) ->
arrayifiedParams = {}
for param in params
paramName = param.name
arrayifiedParams[paramName] = arrayifiedParams[paramName] || []
arrayifiedParams[paramName].push param.value
params.map (param) ->
paramName = param.name
if arrayifiedParams[paramName].length > 1
param.value = arrayifiedParams[paramName]
param
Here is the corresponding JSFiddle.