I am working on Angular 12, in my TS file
I have an array response from a file upload like this-
[
{
"id": "7",
"name": "xyz",
"job": "doctor",
"preference": "1"
},
{
"id": "7",
"name": "xyz",
"job": "nurse",
"preference": "2"
},
{
"id": "7",
"name": "xyz",
"job": "manager",
"preference": "3"
},
{
"id": "7",
"name": "xyz",
"job": "assistant",
"preference": "4"
},
{
"id": "7",
"name": "xyz",
"job": "chairman",
"preference": "5"
},
{
"id": "7",
"name": "xyz",
"job": "designer",
"preference": "6"
}
]
I want to convert this array to an object in JSON like this-
[
{
"id": "7",
"name": "xyz",
"array1": [
{
"date": "today's Date,",
"endDate": "somedate",
"lastUpdatedBy": "someuser",
"array2": [
{
"job": "doctor",
"preference": "1"
},
{
"job": "nurse",
"preference": "2"
},
{
"job": "manager",
"preference": "3"
},
{
"job": "assistant",
"preference": "4"
},
{
"job": "chairman",
"preference": "5"
},
{
"job": "designer",
"preference": "6"
}
]
}
]
}
]
So based on the id and name combination I want to create the object in JSON which will be passed to an API, I've been trying many things by using map or arrays inside array but having a hard time to succeed. Any help would be appreciated. In map it will not take duplicate keys for same id & name combination. I have tried to create a customized object and loop through it but each time I get a separate result. After looping through the array I am trying to build an object like this, but it doesn't produce expected result.
my code-
let object = {
id: data[i].id,
name: data[i].name,
array1:[
{
date: dateParam,
endDate: '',
lastUpdatedBy: 'me',
array2: [
{
job: data[i].job,
preference: data[i].preference
}
]
}
]
};