Issue
I'm working on angular project and I need to be able to extract and modify the formData before posting it to the serverside as JSON.
Source data
{
"BioData":{
"firstname": "Greg",
"lastName": "John"
},
"locationData":{
"locationList":[
{
"address1": "22 roackfield",
"address2": "30 Grandall view",
"ContactData":{
contactList:[
{
phone: "658884888",
extension: "555",
},
{
phone: "658884888",
extension: "555",
}
]
}
},
{
"address1": "102 Mockview",
"address2": "730 Trunder str",
"ContactData":{
contactList:[
{
phone: "9999999",
extension: "333",
},
{
phone: "97874747646",
extension: "000",
}
]
}
}
]
},
}
I have tried forEach and recursive method of accessing object and arrays, not achieving my desired result
Modify Data should be in this format shown below
{
"BioData":{"firstname": "Greg",.......},
"locationData":[
{
"address1: "22 roackfield",
"address2": "30 Grandall view",
"contactList":[{obj1}, {obj2} .....]
},
{
"address1: "22 roackfield",
"address2": "30 Grandall view",
"contactList":[{obj1}, {obj2} .....]
}
]
}
Solution
I have changed few of the property name as the standard naming convention was not followed. If you want to keep them your way you just need to make some simple changes in the code.
I created the following interfaces and made those properties optional so that I don't have to initialize
interface BioData {
firstName?: string,
lastName?: string
}
interface PhoneList {
phone?: string,
extension?: string
}
interface LocationData {
address1?: string,
address2?: string,
phoneList?: PhoneList[]
}
interface EmployeeDetails {
bioData: BioData,
locationData: LocationData[];
}
Based on your requirement I used the following way. It is a crude code written in a short time. Please feel free to tweak around the code if you need to. This is just to give you an idea. Please mark it as answer if it helps you find a way. Happy Coding :)
let result: EmployeeDetails = { bioData: {}, locationData: [] };
result.bioData = { ...actual.bioData };
result.locationData = actual.locationData.locationList.map(x => {
return {
address1: x.address1,
address2: x.address2,
phoneList: x.contactData.contactList
}
});
Answered By - Sudipto Mukherjee
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.