My goal is to map an array of objects. Currently, I have the following code:
return this.service.post(url, payload, this.httpOptions)
.pipe(
map((obj: any, index) => [({
...obj,
val1: obj[index].val1.id,
val2: obj[index].val2.id
})]
))
I've also tried approaching it in a different way:
map((obj: any, index) =>
{
const list = []
const newObj = {
...obj,
val1: obj[index].val1.id,
val2: obj[index].val2.id
}
list.push(newObj);
return list;
}
Input:
[
{
"val1": { "id": "USER_ID", "value": "User1" },
"val2": { "id": "USER_ID", "value": "User2" },
"val3": "aaa",
"val4": "val2"
},
{
"val1": { "id": "USER_ID", "value": "User3" },
"val2": { "id": "USER_ID", "value": "User4" },
"val3": "dds",
"val4": "eee"
}
]
Expected output:
[
{
"val1": "USER_ID",
"val2": "USER_ID",
"val3": "aaa",
"val4": "val2"
},
{
"val1": "USER_ID",
"val2": "USER_ID",
"val3": "dds",
"val4": "eee"
}
]
Currently, instead of an array of objects, I have an array with one object that contains all objects. My desired outcome is to have an array of objects where each object contains the original data plus the mapped values such as val1 and val2.