Reputation: 3100
I'm trying to transform the following input which is an array of JSON to expected output shown below. indexCols is also
var objects = ['{"ticker":"MSFT", "key": 2, "PX_LAST":100}',
'{"ticker":"AAPL", "key": 3, "PX_LAST":100}',
'{"ticker":"GOOG", "key": 4, "PX_LAST":100}']
/* Expected output
[[id, '{"ticker":"MSFT", "key": 2, "PX_LAST":100}', 'MSFT', 2 ]
[id, '{"ticker":"AAPL", "key": 3, "PX_LAST":100}', 'AAPL', 3 ]
[id, '{"ticker":"GOOG", "key": 4, "PX_LAST":100}', 'GOOG', 4 ]]
*/
var indexCols = ['ticker', 'key']
var transform = function(input, indexCols, id) {
var fn2 = R.compose(R.flip(R.map)(indexCols), R.flip(R.prop), JSON.parse)
var fn3 = R.map(R.compose(R.concat([id]), fn2))
return fn3(objects)
}
transform(objects, indexCols, 100)
/* result : [[100, "MSFT", 2], [100, "AAPL", 3], [100, "GOOG", 4]] */
As you can see the result arrays are missing the second element which is the json string. I'm unable to weave that in a functional way.
Upvotes: 0
Views: 268
Reputation: 14423
You can use R.converge
so you can feed the string to concat and JSON.parse:
var objects = ['{"ticker":"MSFT", "key": 2, "PX_LAST":100}',
'{"ticker":"AAPL", "key": 3, "PX_LAST":100}',
'{"ticker":"GOOG", "key": 4, "PX_LAST":100}']
/* Expected output
[[id, '{"ticker":"MSFT", "key": 2, "PX_LAST":100}', 'MSFT', 2 ]
[id, '{"ticker":"AAPL", "key": 3, "PX_LAST":100}', 'AAPL', 3 ]
[id, '{"ticker":"GOOG", "key": 4, "PX_LAST":100}', 'GOOG', 4 ]]
*/
var indexCols = ['ticker', 'key']
var transform = function(input, indexCols, id) {
//Have to trick R.converge into making it think that JSON.parse only has 1 argument
var fn2 = R.compose(R.flip(R.map)(indexCols), R.flip(R.prop), R.curryN(1, JSON.parse))
var fn3 = R.map(R.compose(R.concat([id]), R.converge(R.concat, [Array, fn2])))
return fn3(input) //typo shouldn't be objects
}
console.log(transform(objects, indexCols, 100))
/* result : [[100, "MSFT", 2], [100, "AAPL", 3], [100, "GOOG", 4]] */
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.min.js"></script>
An alternative could be to use:
R.ap(R.compose(R.concat, Array), fn2))
// concat <$> Array <*> fn2
// R.liftN(2, R.concat)(Array, fn2) is the same
Instead of:
R.converge(R.concat, [Array, fn2])
That way you don't run into R.converge
issue where it's guessing JSON.parse
has 2 arguments.
Upvotes: 1
Reputation: 2195
These look unnecessarily complex...
How about:
const objects = ['{"ticker":"MSFT", "key": 2, "PX_LAST":100}',
'{"ticker":"AAPL", "key": 3, "PX_LAST":100}',
'{"ticker":"GOOG", "key": 4, "PX_LAST":100}']
const indexCols = ['ticker', 'key', 'PX_LAST']
const pickIndeces = pipe(JSON.parse, props(indexCols))
const format = map(x => concat([x], pickIndeces(x)))
format(objects)
Note, pipe
, props
, concat
and map
are Ramda functions. So you can import them, like so:
const { concat, map, pipe, props } = R
Upvotes: 0
Reputation: 361
( I think there is a typo, instead of this: return fn3(objects) you probably mean this: return fn3(input) )
var transform = function(input, indexCols, id) {
var fn2 = R.compose(R.flip(R.map)(indexCols), R.flip(R.prop), JSON.parse);
var valueToArray = R.flip(R.append)([]);
var fn3 = R.map(R.compose(R.flatten, R.concat([id]), R.compose(R.ap([R.identity, fn2]), valueToArray)))
return fn3(input)
}
https://codepen.io/anon/pen/Jrzmda
You have to duplicate your input object (a->[a,a]), then you can operate on the second element as you like (fn2), just keep the first element untouched (identity), and, at the end, concat it all together.
Improvement 1: Is there a Ramda function for valueToArray ( a->[a] ) already? Improvement 2: Is there a way to concat id not nested to get rid of the R.flatten?
Upvotes: 1