I have a dataset of JSON entries:
[{"num": "1","name_A": "Alex" ,"name_B": "Bob"}, {"num": "2","name_A": "Anne" ,"name_B": "Barbra"}]
My goal is to effortlessly convert this collection of Objects into two separate objects - one labeled name_A, and the other as name_B. Each object should consist of the label and an array of corresponding num-name pairs:
[{title: "name_A", names:[{"1", "Alex}, {"2", "Anne"}]}, {title:"name_B", names: [{"1", "Bob"}, {"2", "Barbra"}]}]
Initially, I attempted to create the two objects by using array reduction twice, first for name_A and then for name_B before merging them together:
// obtain 'names' array
var name_A = objArray.reduce(function(memo, curr) {
memo.push({curr.num, curr.name_A})
return memo;
}, []);
However, my approach seems to be failing. Why is there no push method for memo when initializing reduce with an empty array?
Furthermore, am I steering in the right direction or is there a more efficient way to accomplish this task?