I have a requirement where one needs to inflate a form-specific flat data-structure. Before sending the user entered values, I need to build a data-structure according to what the server's API does expect.
Say I have following key-value pairs from a user's input
{
state: "California",
city: "New York",
full_name: "rererer",
email: "[email protected]",
dob: "1/1/2014",
pincode: "222222",
gender: "male",
}
The data API does require a nested object where
- an
Addresswill includestate,cityandfull_name - and a
Profilehas to featureemail,dob,pincodeandgender.
How does one convert the above key-value pairs into following nested data-structure?
{
Address: {
state: "California",
city: "New York",
full_name: "rererer",
},
Profile: {
email: "[email protected]",
dob: "1/1/2014",
pincode: "222222",
gender: "male",
},
}
Simple example is that you can store the json in a variable as X wherein you can create 2 object Address and Profile then put the required fields from the X variable to its respective objects and later merge that object together.
Example:
you can automate this code