i want to retrieve data from a first JSON object (obj1) and fill a second JSON object (obj2) but they didn't have the same structure for example:
obj1 = [
{
"fieldType": "FormField",
"id": "creationdunutilisateur",
"name": "Creation d'un Utilisateur",
"type": "headline",
"value": null,
"required": false,
"readOnly": false,
"overrideId": false,
"placeholder": null,
"layout": null
},
{
"fieldType": "FormField",
"id": "usernameemail",
"name": "UserName/Email",
"type": "text",
"value": null,
"required": true,
"readOnly": false,
"overrideId": false,
"placeholder": "Veuillez saisir votre UserName/Email",
"layout": null
},
{
"fieldType": "FormField",
"id": "password",
"name": "Password",
"type": "password",
"value": null,
"required": true,
"readOnly": false,
"overrideId": false,
"placeholder": "Veuillez saisir votre password",
"layout": null
},
{
"fieldType": "FormField",
"id": "createduser",
"name": "createduser",
"type": "functional-group",
"value": null,
"required": false,
"readOnly": false,
"overrideId": false,
"placeholder": null,
"layout": null
}
]
obj2 i mean the destination object have the following structure :
[
{
"type": "input", // type of obj1
"inputType": "number",//type of obj1
"id": "employeeNumber",// id of obj1
"label": "Employee Number",// value of obj1
"appearance": "outline",
"validators": [
{
"name": "required",
"args": null,
"propertyName": "required",
"message": "placeholder.toString()"// placeholder of obj1
}
],
"gridItemClass": "grid-column-1",
"required": true // required of obj1
}
......
..... ect...
]
i want to know if there is any function to do this operation ,thank you very much.
CodePudding user response:
There is no simple one-size-fits-all function for mapping data.
When it comes to data mapping, there is "no free lunch", meaning it is a hard problem without simple shortcuts. You have to know which data is mapped to what and code it.
You might find some JavaScript features useful though, especially destructuring.
CodePudding user response:
A good ol array.map
gets you there
obj2Array = obj1Array.map((obj1) =>{
return {
type: obj1.type,
required: obj1.required,
validators: obj1.placeholder && [ //placeholder is null of obj1.placeholder is null
{
"name": "required",
"args": null,
"propertyName": "required",
"message": obj1.placeholder
}
],
//other fields
}
})