I have an array of objects like in the following structure.
[
{ TaskID: "1001", TaskName: "task1", AID: "10"},
{ TaskID: "1002", TaskName: "task2", AID: "10"}
]
I have to create an array from the above array to the following structure.
[
{
"TaskID": 1,
"Value": "1001",
"AID": 10
},
{
"TaskID": 2,
"Value": "task1",
"AID": 10
},
{
"TaskID": 1,
"Value": "1002",
"AID": 10
},
{
"TaskID": 2,
"Value": "task2",
"AID": 10
},
]
I've succeeded by using this below code but not sure it is the best method.
let taskIdArray = this.taskArray.map((tasks) => {
return {
TaskID: 1,
Value: tasks.tID,
AId: tasks.AID
}
})
let taskNameArray = this.taskArray.map((tasks) => {
return {
TaskID: 2,
Value: tasks.Name,
AId: tasks.AID
}
})
this.finalTaskArray = taskIdArray.concat(taskNameArray);
Can anyone help me to optimize the code and suggest a better method if any?