我从 API 调用中得到以下响应,我刚刚将其分配给一个变量finalUpdate以供工作。
var finalUpdate = {
creator: {
"firstName": "Cruise",
"lastName": "Tom",
"email": "tom.cruise@abc.com"
},
field: [{
key: 'Car',
value: {
"tom.cruise@abc.com": {
"userType": "responser",
"data": [{
"id": "98d0c05a-cd58-43b6-9465-370871a1b5ad",
"data": {
"value": "FORD FIESTA "
}
}],
"time": 1631637403438
}
},
editable: 'true',
type: 'inputbox'
}, {
key: 'Car',
value: {
"tom.cruise@abc.com ": {
userType: "responser",
data: [{
id: "98d0c05a-cd58-43b6-9465-370871a1b5ad",
data: {
value: "BMW 116i"
}
}],
time: 1631610481102
}
},
editable: "true",
type: "inputbox"
},
{
key: 'Car',
value: {
"tom.cruise@abc.com ": {
userType: "responser",
data: [{
id: "98d0c05a-cd58-43b6-9465-370871a1b5ad",
data: {
value: "Audi Q8"
}
}],
time: 1631610489632
}
},
editable: "true",
type: "inputbox"
}
],
response: [{
"email": "tom.cruise@abc.com",
"firstName": "Cruise",
"lastName": "Tom",
"id": 1
},
{
"email": "tom.cruise@abc.com",
"firstName": "Cruise",
"lastName": "Tom",
"id": 1
},
{
"email": "tom.cruise@abc.com",
"firstName": "Cruise",
"lastName": "Tom",
"id": 1
}
],
title: 'EEEEE WORK',
collectionID: '1234'
}
我正在尝试将上述结果修改为所需的输出,如下所示: 此数据的背景是,我有 1 个用户,他修改了数据字段 3 次,用户值可以在属性中看到value。
//Final Formatted Output needed
{
'tom.cruise@abc.com': [{
key: 'Car',
value: {
"id": "98d0c05a-cd58-43b6-9465-370871a1b5ad",
"data": {
"value": "FORD FIESTA "
}
},
editable: 'true',
type: 'inputbox',
time: 1631637403438
}],
'tom.cruise@abc.com': [{
key: 'Car',
value: {
"id": "98d0c05a-cd58-43b6-9465-370871a1b5ad",
"data": {
"value": "BMW 116i "
}
},
editable: 'true',
type: 'inputbox',
time: 1631610481102
}],
'tom.cruise@abc.com': [{
key: 'Car',
value: {
"id": "98d0c05a-cd58-43b6-9465-370871a1b5ad",
"data": {
"value": "Audi Q8 "
}
},
editable: 'true',
type: 'inputbox',
time: 1631610489632
}],
}
我的工作代码如下所示,我尝试获得最接近的结果,但无法根据需要将用户映射到值.....
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);
但是我无法将电子邮件或用户映射到结果。如何将用户添加到获得的特定结果中。上述代码的结果如下:
"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" }]