获取总聊天记录和错过的聊天记录



我有一个这样的样本数据。

{
"websiteId": "4f8b36d00000000000000001",
"date": "2019-04-01T00:00:00.000Z",
"chats": 121,
"missedChats": 0
},
{
"websiteId": "4f8b36d00000000000000002",
"date": "2019-04-01T00:00:00.000Z",
"chats": 13,
"missedChats": 0
},
{
"websiteId": "4f8b36d00000000000000003",
"date": "2019-04-01T00:00:00.000Z",
"chats": 232,
"missedChats": 9
},
{
"websiteId": "4f8b36d00000000000000004",
"date": "2019-04-01T00:00:00.000Z",
"chats": 9,
"missedChats": 0
},
{
"websiteId": "4f8b36d00000000000000005",
"date": "2019-04-01T00:00:00.000Z",
"chats": 0,
"missedChats": 0
},
{
"websiteId": "4f8b36d00000000000000006",
"date": "2019-04-01T00:00:00.000Z",
"chats": 0,
"missedChats": 0
},
{
"websiteId": "4f8b36d00000000000000007",
"date": "2019-04-01T00:00:00.000Z",
"chats": 0,
"missedChats": 1
},
{
"websiteId": "4f8b36d00000000000000008",
"date": "2019-04-01T00:00:00.000Z",
"chats": 0,
"missedChats": 0
},
{
"websiteId": "4f8b36d00000000000000007",
"date": "2019-04-02T00:00:00.000Z",
"chats": 0,
"missedChats": 1
},
{
"websiteId": "4f8b36d00000000000000006",
"date": "2019-04-02T00:00:00.000Z",
"chats": 0,
"missedChats": 1
}

我代码:

const dataset = [];
const websiteIds = [];
const dates = [];
const chats = [];
const missedChats = [];
let chatsSum = 0;
let missedChatsSum = 0;
function getData() {
if (start == null && end == null) {
fetch(url)
.then(res => res.json())
.then(data => {
data.map(result => {
console.log(result)
let sets = {
websiteId: result.websiteId,
chats: result.chats,
missedChats: result.chats,
}

chats.push(result.chats)
missedChats.push(result.missedChats)

if (!websiteIds.includes(result.websiteId)) {
websiteIds.push(result.websiteId)
for (let x = 0; x < websiteIds.length; x++) {
if (sets.websiteId == websiteIds[x]) {
dataset.push(sets)
}
if (result.websiteId == websiteIds[x]) {
dataset[x].chats += result.chats
dataset[x].missedChats += result.missedChats
}
}
}
})
})

} else {
console.log('date given')
}
}

我期望的输出应该是这样的

{
"websiteId": "4f8b36d00000000000000001",
"date": "2019-04-01T00:00:00.000Z",
"chats": 500, // total number of chat in this websiteID
"missedChats": 15 // total number of chat in this websiteID
},
{
"websiteId": "4f8b36d00000000000000002",
"date": "2019-04-02T00:00:00.000Z",
"chats": 100, // total number of chat in this websiteID
"missedChats": 50// total number of chat in this websiteID
},
{
"websiteId": "4f8b36d00000000000000003",
"date": "2019-04-02T00:00:00.000Z",
"chats": 200, // total number of chat in this websiteID
"missedChats": 25 // total number of chat in this websiteID
}

我试图得到聊天和错过聊天的总和每我的网站id。是否有一种方法,如何我可以得到的数据上给定的例子?当我试图运行我的代码,它不总结聊天和错误,它只得到每个网站id的第一个值。

您可以使用reduce来对这些值求和:

function sum(data) {
return Object.values(data.reduce((acc, el) => {
if (!(el.websiteId in acc)) {
acc[el.websiteId] = el;
} else {
acc[el.websiteId].chats += el.chats;
acc[el.websiteId].missedChats += el.missedChats;
}
return acc;
}, {}));
}

const data = [{
"websiteId": "4f8b36d00000000000000001",
"date": "2019-04-01T00:00:00.000Z",
"chats": 121,
"missedChats": 0
},
{
"websiteId": "4f8b36d00000000000000002",
"date": "2019-04-01T00:00:00.000Z",
"chats": 13,
"missedChats": 0
},
{
"websiteId": "4f8b36d00000000000000003",
"date": "2019-04-01T00:00:00.000Z",
"chats": 232,
"missedChats": 9
},
{
"websiteId": "4f8b36d00000000000000004",
"date": "2019-04-01T00:00:00.000Z",
"chats": 9,
"missedChats": 0
},
{
"websiteId": "4f8b36d00000000000000005",
"date": "2019-04-01T00:00:00.000Z",
"chats": 0,
"missedChats": 0
},
{
"websiteId": "4f8b36d00000000000000006",
"date": "2019-04-01T00:00:00.000Z",
"chats": 0,
"missedChats": 0
},
{
"websiteId": "4f8b36d00000000000000007",
"date": "2019-04-01T00:00:00.000Z",
"chats": 0,
"missedChats": 1
},
{
"websiteId": "4f8b36d00000000000000008",
"date": "2019-04-01T00:00:00.000Z",
"chats": 0,
"missedChats": 0
},
{
"websiteId": "4f8b36d00000000000000007",
"date": "2019-04-02T00:00:00.000Z",
"chats": 0,
"missedChats": 1
},
{
"websiteId": "4f8b36d00000000000000006",
"date": "2019-04-02T00:00:00.000Z",
"chats": 0,
"missedChats": 1
}];
console.log(sum(data));

data.reduce((acc, el) => {
if (!(el.websiteId in acc)) {
acc[el.websiteId] = el;
} else {
acc[el.websiteId].chats += el.chats;
acc[el.websiteId].missedChats += el.missedChats;
}
return acc;
}, {})

创建一个对象,使用websiteId作为包含求和值的键。Object.values以数组形式返回值

最新更新