I have a kind of complicated algorithm to write using Javascript.
I have a data structure like this:
const data = [
{
some_fees_dent_0: true,
some_fees_name_0: "junao",
some_fees_name_1: "adhm",
some_fees_name_2: "uio",
some_fees_rate_0: "45",
some_fees_rate_1: "1",
some_fees_rate_2: "22",
some_fees: [], # HERE
initial_fees: [], # HERE
initial_fees_dent_0: true,
initial_fees_name_0: "james",
initial_fees_name_1: "daiep",
initial_fees_name_2: "moaip",
initial_fees_rate_0: "7",
initial_fees_rate_1: "11",
initial_fees_rate_2: "88",
initial_fees_type_0: "foo",
initial_fees_type_1: "bar",
initial_fees_type_2: "random",
}
]
I want to achieve this:
const data = [
{
some_fees: [
{
some_fees_name: "junao", # was initially some_fees_name_0
some_fees_rate: "45", # was initially some_fees_rate_0
some_fees_dent: true, # was initially some_fees_dent_0
},
{
some_fees_name: "adhm", # was initially some_fees_name_1
some_fees_rate: "1", # was initially some_fees_rate_1
},
{
some_fees_name: "uio", # was initially some_fees_name_2
some_fees_rate: "22", # was initially some_fees_rate_2
},
],
initial_fees: [
{
initial_fees_name: "james", # was initially initial_fees_name_0
initial_fees_rate: "7", # was initially initial_fees_rate_0
initial_fees_type: "foo", # was initially initial_fees_type_0
initial_fees_dent: true, # was initially initial_fees_dent_0
},
{
initial_fees_name: "daiep", # was initially initial_fees_name_1
initial_fees_rate: "11", # was initially initial_fees_rate_1
initial_fees_type: "bar", # was initially initial_fees_type_1
},
{
initial_fees_name: "moaip", # was initially initial_fees_name_2
initial_fees_rate: "88", # was initially initial_fees_rate_2
initial_fees_type: "random", # was initially initial_fees_type_3
},
],
}
]
Basically what happened is the initial data
array that include objects now only include only two fields: some_fees
initial_fees
.
The other fields were put together in the same object based on the last number on their key
, Which can be considered as index
differentiator.