I get the below response from an API call, which i have just assigned to a variable finalUpdate
for working.
var finalUpdate = {
creator: {
"firstName": "Cruise",
"lastName": "Tom",
"email": "[email protected]"
},
field: [{
key: 'Car',
value: {
"[email protected]": {
"userType": "responser",
"data": [{
"id": "98d0c05a-cd58-43b6-9465-370871a1b5ad",
"data": {
"value": "FORD FIESTA "
}
}],
"time": 1631637403438
}
},
editable: 'true',
type: 'inputbox'
}, {
key: 'Car',
value: {
"[email protected] ": {
userType: "responser",
data: [{
id: "98d0c05a-cd58-43b6-9465-370871a1b5ad",
data: {
value: "BMW 116i"
}
}],
time: 1631610481102
}
},
editable: "true",
type: "inputbox"
},
{
key: 'Car',
value: {
"[email protected] ": {
userType: "responser",
data: [{
id: "98d0c05a-cd58-43b6-9465-370871a1b5ad",
data: {
value: "Audi Q8"
}
}],
time: 1631610489632
}
},
editable: "true",
type: "inputbox"
}
],
response: [{
"email": "[email protected]",
"firstName": "Cruise",
"lastName": "Tom",
"id": 1
},
{
"email": "[email protected]",
"firstName": "Cruise",
"lastName": "Tom",
"id": 1
},
{
"email": "[email protected]",
"firstName": "Cruise",
"lastName": "Tom",
"id": 1
}
],
title: 'EEEEE WORK',
collectionID: '1234'
}
I am trying to modify the above result to thedesired output as seen below: Background of this data would be, I have 1 user, who has modified a data field 3 times, the user value can be seen in the attribute value
.
//Final Formatted Output needed
{
'[email protected]': [{
key: 'Car',
value: {
"id": "98d0c05a-cd58-43b6-9465-370871a1b5ad",
"data": {
"value": "FORD FIESTA "
}
},
editable: 'true',
type: 'inputbox',
time: 1631637403438
}],
'[email protected]': [{
key: 'Car',
value: {
"id": "98d0c05a-cd58-43b6-9465-370871a1b5ad",
"data": {
"value": "BMW 116i "
}
},
editable: 'true',
type: 'inputbox',
time: 1631610481102
}],
'[email protected]': [{
key: 'Car',
value: {
"id": "98d0c05a-cd58-43b6-9465-370871a1b5ad",
"data": {
"value": "Audi Q8 "
}
},
editable: 'true',
type: 'inputbox',
time: 1631610489632
}],
}
My workcode is shown below, I tried getting the closest result but not able to map user to the value as desired.....
var resp = finalUpdate.response;
var emails = [];
var fields = [];
//Fetching the emails to match with field List
resp.map((x) => {
emails.push(x.email)
});
console.log("Email ==>", emails);
//Checking the field length
console.log("Field length", finalUpdate.field.length);
finalUpdate.field.map((x) => {
var data = x.value;
var keys = Object.keys(data)
console.log("data", data);
console.log("keys", keys);
//For each iteraton map corresponding email with field value email and format output as shown below
keys.map((key) => {
console.log("data of specifi user", data[key]);
fields.push({
key: x.key,
value: data[key].data,
time: data[key].time,
editable: x.editable,
type: x.type
});
});
});
console.log("Fields", fields);
However I am unable to map the email or user to the result. How do i add the user to the specific result obtained. The result of the above code is as follows:
"Fields" Array [Object { key: "Car", value: Array [Object { id: "98d0c05a-cd58-43b6-9465-370871a1b5ad", data: Object { value: "FORD FIESTA " } }], time: 1631637403438, editable: "true", type: "inputbox" }, Object { key: "Car", value: Array [Object { id: "98d0c05a-cd58-43b6-9465-370871a1b5ad", data: Object { value: "BMW 116i" } }], time: 1631610481102, editable: "true", type: "inputbox" }, Object { key: "Car", value: Array [Object { id: "98d0c05a-cd58-43b6-9465-370871a1b5ad", data: Object { value: "Audi Q8" } }], time: 1631610489632, editable: "true", type: "inputbox" }]