I have an array of such objects:
var data = [ { type : "parent", name : "A" }, { type : "child", name : "1" }, { type : "child", name : "2" }, { type : "parent", name : "B" }, { type : "child", name : "3" } ]
and I want to move the child objects to the parent objects divided by the parrent object (there is no specified key from the child object belonging to that partin). Thus, it is shared only by the parent. To be simple, I want to change the array to:
[ { type : "parent", name : "A", child: [ { type : "child", name : "1" }, { type : "child", name : "2" } ] }, { type : "parent", name : "B", child: [ { type : "child", name : "3" } ] } ]
I read lodash about chunk , but it's useless.
source share