How can we modify an array of objects in Angular Typescript by replacing certain values and adding new key-value pairs?
I need to swap the id value with memberId and then introduce a new key fullName that combines firstName and lastName.
Any suggestions or assistance would be greatly appreciated. Thanks!
#Current solution
this.data = this.data.map((item) => {
let id = item.memberId;
return {
...item,
}
});
#Original Data
[
{
"id": 10017,
"firstName": "Alexa",
"lastName": "Rimura",
"roleDisplay": "COVP,DVP Real Estate",
"companyName": "MS",
"title": "COO",
"memberId": 1,
"roleDto": [
{
"id": 6,
"name": "COVP",
"isShow": true,
"transactionRoleId": 9
},
{
"id": 7,
"name": "DVP Real Estate",
"isShow": true,
"transactionRoleId": 6
}
],
"transactionRoleList": "Architect, Construction Project Director"
},
{
"id": 10018,
"firstName": "Briana",
"lastName": "Christoval",
"roleDisplay": "Architect,Construction Project Director",
"companyName": null,
"title": null,
"memberId": 2,
"roleDto": [
{
"id": 8,
"name": "Architect",
"isShow": true,
"transactionRoleId": 12
},
{
"id": 9,
"name": "Construction Project Director",
"isShow": true,
"transactionRoleId": 11
}
]
}
]
#Expected Output
[
{
"id": 1,
"firstName": "Alexa",
"lastName": "Rimura",
"fullName": "Alexa Rimura" ,
"roleDisplay": "COVP,DVP Real Estate",
"companyName": "MS",
"title": "COO",
"memberId": 1,
"roleDto": [
{
"id": 6,
"name": "COVP",
"isShow": true,
"transactionRoleId": 9
},
{
"id": 7,
"name": "DVP Real Estate",
"isShow": true,
"transactionRoleId": 6
}
],
"transactionRoleList": "Architect, Construction Project Director"
},
{
"id": 2,
"firstName": "Briana",
"lastName": "Christoval",
"fullName": "Briana Christoval",
"roleDisplay": "Architect,Construction Project Director",
"companyName": null,
"title": null,
"memberId": 2,
"roleDto": [
{
"id": 8,
"name": "Architect",
"isShow": true,
"transactionRoleId": 12
},
{
"id": 9,
"name": "Construction Project Director",
"isShow": true,
"transactionRoleId": 11
}
]
}
]