I have parsed the array of json object into a particular format but i need the "type" property value of object to to be dynamic based on condition.
input >>
[
{
"accident_description": "bike accident",
"reported_by": "john",
},
{
"accident_description": "car accident",
"reported_by": "sam",
}
]
output >>
"fields": [
{
"title": "accident_description",
"values": "bike accident"
"type": "generic",
},
{
"title": "reported_by",
"values": "john",
"type": "generic",
},
{
"title": "accident_description",
"values": "car accident"
"type": "generic",
},
{
"title": "reported_by",
"values": "sam",
"type": "generic",
},
]
I have tried this and it is working fine
const arr = [ { "accident_description": "bike accident", "reported_by": "john", }, { "accident_description": "car accident", "reported_by": "sam", } ];
let res = arr.flatMap(x => (Object.entries(x).map(([k,v]) => ({title:k,values:v,type:"generic"}))));
console.log(res);
but here type is fixed ,i need "type" value to dynmaic based on the condition given below.
if(title=='accident_description')
type:generic
else
type:custom