I need to display some data on a grid but in a different format than I am fetching it in and having a difficult time figuring out how to refactor it. I am grabbing a set of javascript objects which contain a subscription length and a price along with other details. All objects whose values in the other columns are a match, should be combined into 1 object. The new object should have the same columns which the old items had except for subscription length and price. These should become key value pairs on the new object. An example:
[
{
col_a:"value",
col_b:"foo",
col_c: true,
subscription_length:3,
price:10
},
{
col_a:"value",
col_b:"foo",
col_c: true,
subscription_length:6,
price:18
},
{
col_a:"value",
col_b:"foo",
col_c: true,
subscription_length:12,
price:32
},
{
col_a:"something",
col_b:"bar",
col_c: false,
subscription_length:3,
price:8
},
{
col_a:"something",
col_b:"bar",
col_c: false,
subscription_length:6,
price:15
},
{
col_a:"something",
col_b:"bar",
col_c: false,
subscription_length:12,
price:28
}
]
would become:
[
{
col_a:"value",
col_b:"foo",
col_c: true,
3:10,
6:18,
12:32
},
{
col_a:"something",
col_b:"bar",
col_c: false,
3:8,
6:15,
12:28
}
]
I assume I can use a map function possibly with a reduce, but I don't know where to start. Thanks for any and all pointers!