如何比较然后在js中添加特定对象的字段?

每次我击中botton时,都会向cookie中添加一个ID,因此该值(与该ID对应)可以重复。然后,我在Express中读取了所有元素(模型中的价格称为price,因此来自MongoDB和cookie的works.price。 我的目标是在PUG中传递数据,那里有MongoDB中的元素列表,没有重复项,但mongoDB元素中存在works.price的总和。

exports.getcart = catchAsync(async (req, res, next) => {
const works = await Work.find();
const cookies = req.cookies;
const fullUrl = req.protocol + `://${req.get('host')}${req.originalUrl}`;

const urlCookie = fullUrl.split('/')[2];
delete cookies['urlCookie'];

let value = {};
let items = [];
let id = [];
let counts = {};

for (let key in cookies) {
    const work = await Work.findById({ _id: cookies[key] });
    items.push(value.work = work);
    id.push(cookies[key]);
}
const unique = [...new Set(id)];
id.forEach(function (x) {
    counts[x] = (counts[x] || 0) + 1;

});
console.log(items);
console.log(unique);
console.log(id);



// console.log(counts);

res.status(200).render('page', {
    title: 'page',
    <OBJECT?>
    });
});

这是我正在做的console.log。 在我称之为项目的数组中,有一个具有相同ID的对象数组存在于cookie中。

console.log(unique);
    [
  '5eb2f7efb5b8fa62bcd7db94',
  '5eb19f3bad682508dc024301',
  '5eb568d4c60f2471684ee9fb'
]

console.log(id);
[
  '5eb2f7efb5b8fa62bcd7db94',
  '5eb19f3bad682508dc024301',
  '5eb19f3bad682508dc024301',
  '5eb568d4c60f2471684ee9fb',
  '5eb568d4c60f2471684ee9fb',
  '5eb19f3bad682508dc024301',
  '5eb2f7efb5b8fa62bcd7db94'
]
评论