Currently, I am in the process of transitioning a series of functions from _.chain to _fp.flow. However, I am encountering some challenges when dealing with currying complex objects within the flow. My goal is to:
- Transform an array of objects using grouped functions like
countBy
or
)sumBy</code) into a dictionary format (e.g. <code>{ group1:10, group2:15... }
- Convert this dictionary into an array of key/value pairs (e.g.
)[{column: 'group1', value: '10'}, ...]
- Sort the data based on a certain variable in either ascending or descending order
However, despite my efforts, the output object ends up being flattened into a lengthy array. The code snippet below shows part of the problem. While the reducer function successfully groups the values as intended, it seems that the interaction between the each step and orderBy is somehow flattening the object (the desired object structure appears correct after the _.each console.log).
I have included a sample of the code in the linked JSFiddle.
const inData = [{
target: 123,
groupby: 'a'
},...
}];
const colData = _.flow(
_.reduce(reducer, {}),
_.toPairs,
_.each(([value, column]) => {
console.log(value);
console.log(column);
const outObj = {
value: value,
column: column
}
console.log(outObj)
return (outObj);
}),
_.orderBy(['value'], [sortDir]),
// Have tried result with or without fromPairs
_.fromPairs
)(inData);
On a side note, I utilize ES6 syntax and React in my main project, which may impact the approach I take towards solving this issue.