Quite literally, actually:
_.chain(arr)
.invoke("split", ".&")
.groupBy(0)
.map(function(els) {
return "{\n\t"+ _.invoke(els, "join", ".&").join(", ") + "\n}";
})
.value().join(",\n");
Question
I have a JavaScript array which looks like this:
var arr = ["[Dim1].[Mem1].&[1]",
"[Dim2].[Mem1].&[2]",
"[Dim1].[Mem1].&[5]",
"[Dim2].[Mem1].&[77]",
"[Dim3].[Mem1].&[1]"
]
I want output in this format:
{
[Dim1].[Mem1].&[1], [Dim1].[Mem1].&[5]
},
{
[Dim2].[Mem1].&[2], [Dim2].[Mem1].&[77]
},
{
[Dim3].[Mem1].&[1]"]
}
The idea is to split each value based on '.&' and then group by the result[0].
I am able to do this by:
How can I achieve this using "lodash" or any other JS based library which will reduce the code and make the method compact?
Solution
Quite literally, actually:
_.chain(arr)
.invoke("split", ".&")
.groupBy(0)
.map(function(els) {
return "{\n\t"+ _.invoke(els, "join", ".&").join(", ") + "\n}";
})
.value().join(",\n");