gpt4 book ai didi

javascript - 如何通过值比较合并两个数组 - Google Analytics 和 MongoDB

转载 作者:太空宇宙 更新时间:2023-11-03 23:49:24 24 4
gpt4 key购买 nike

Stack,这份工作让我绝望。我在忙什么?我使用 Google Analytics NodeJS SDK 来获取网站访问量最大的页面。我让 Google 为我提供用户友好的 URL(slugs)来搜索 MongoDB 数据库。因此,我将访问次数最多的页面的三个 slugs 存储在一个数组中,然后运行正常的 MongoDB Recording.find({slug: searchArr}功能。分析结果可在 result.data.rows 中找到。 .

google.analytics('v3').data.ga.get(
{
auth: jwt,
ids: 'ga:' + view_id,
'start-date': '30daysAgo',
'end-date': 'today',
metrics: 'ga:uniquePageviews',
dimensions: 'ga:pagePath',
sort: '-ga:uniquePageviews',
filters: 'ga:pagePath=~/recordings/',
'max-results': '3'
},
(err, result) => {
if (err) {
Sentry.captureException(err);
res.json({ success: false, message: 'An error occurred.' });
} else {
let mostViewedFirst = {
slug: result.data.rows[0][0].replace('/recordings/', ''),
views: result.data.rows[0][1]
};
const mostViewedSecond = {
slug: result.data.rows[1][0].replace('/recordings/', ''),
views: result.data.rows[1][1]
};
const mostViewedThird = {
slug: result.data.rows[2][0].replace('/recordings/', ''),
views: result.data.rows[2][1]
};
let searchArr = [mostViewedFirst.slug, mostViewedSecond.slug, mostViewedThird.slug];
let concatArr = [
{
slug: mostViewedFirst.slug,
views: mostViewedFirst.views
}, {
slug: mostViewedSecond.slug,
views: mostViewedSecond.views
}, {
slug: mostViewedThird.slug,
views: mostViewedThird.views
}];
Recording.find({slug: searchArr},
(err, recording) => {
if (err) {
Sentry.captureException(err);
res.json({ success: false, message: 'Most viewed recording could not be loaded.' });
} else {
res.json(recording.concat(concatArr));
}
}
).populate('genres')
.populate('artists')
.populate('show');
}
});

Google Analytics SDK 响应:

  {
"slug": "recording-a",
"views": "148"
},
{
"slug": "recording-b",
"views": "75"
},
{
"slug": "recording-c",
"views": "68"
}

Mongoose 响应:

  {
"slug": "recording-a",
"title": "Recording A"
...
},
{
"slug": "recording-b",
"title": "Recording B"
...
},
{
"slug": "recording-c",
"title": "Recording C",
...
}

在下一步中,我想将 Google 输出的数组应用到 MongoDB 数据库的数组。值“slug”应该匹配,并且“views”应该插入到相应的对象中。

res.json(recording.concat(concatArr));我能够在 JSON 响应中显示这两个对象:

  {
"slug": "recording-a",
"views": "148"
},
{
"slug": "recording-b",
"views": "75"
},
{
"slug": "recording-c",
"views": "68"
},
{
"slug": "recording-a",
"title": "Recording A"
...
},
{
"slug": "recording-b",
"title": "Recording B"
...
},
{
"slug": "recording-c",
"title": "Recording C",
...
}

我想要的结果:

  {
"slug": "recording-a",
"title": "Recording A"
"views": "148"
},
{
"slug": "recording-b",
"title": "Recording B"
"views": "75"
},
{
"slug": "recording-c",
"title": "Recording C",
"views": "68"
}

不幸的是,我无法将 Analytics 值数组从 Google 获取到 MongoDB 数组。也有可能我写了一些非常复杂的代码——也许整个逻辑可以通过 for 循环来简化。我愿意寻求任何帮助,谢谢!

最佳答案

试试这个:

var googleArray = [  {
"slug": "recording-a",
"views": "148"
},
{
"slug": "recording-b",
"views": "75"
},
{
"slug": "recording-c",
"views": "68"
}];

var mongoArray = [ {
"slug": "recording-a",
"title": "Recording A"
},
{
"slug": "recording-b",
"title": "Recording B"
},
{
"slug": "recording-c",
"title": "Recording C"
}];

// This is the important line:
var mergedArray = googleArray.map(x => Object.assign(x, mongoArray.find(y => y.id == x.id)));

// This is just for showing the output in this example:
alert(JSON.stringify(mergedArray, null, 2));

关于javascript - 如何通过值比较合并两个数组 - Google Analytics 和 MongoDB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59938748/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com