gpt4 book ai didi

javascript - 两个对象和数组 foreach 将旧对象的值放入 typescript 中的新对象

转载 作者:行者123 更新时间:2023-12-02 22:20:04 25 4
gpt4 key购买 nike

我有两个对象和一个arrar

var collection = [
{ _id: 'recommend', count: 3 },
{ _id: 'enjoy', count: 1 },
{ _id: 'contribute', count: 1 }
]
var collection1 = [
{
_id: 'ZWx0aWVtcG86MTIzNDU6ZXNwYW5nbGlz',
websiteId: 'eltiempo',
sectionSlug: 'bogota',
contentId: '12345',
reactionId: 'recommend',
userName: 'espanglis'
}
]
var reactionsId = ['enjoy','recommend','contribute']

我最初的想法是,对于每个reactionsId,从集合中获取值,如果存在,则输入计数,如果不存在,则输入计数0,如果集合1中存在值,则选择true,如果不存在,则输入false,最终结果将是,但我的代码如下,我有问题

[
{
"reactionId": "recommend",
"count": 3,
"selected": true
},
{
"reactionId": "enjoy",
"count": 1,
"selected": false
},
{
"reactionId": "contribute",
"count": 1,
"selected": false
}
]

这是我的代码

  var selected: any
let data: any = []
for (var reactionvaluser of collection1) {
var idreaction = reactionvaluser.reactionId
}
function isEmpty(obj:any) {
for(var key in obj) {
if(obj.hasOwnProperty(key))
return false;
}
return true;
}
let valores: any

for (let reactionval of collection) {
selected = reactionval._id.includes(idreaction)
reactionsId.forEach(function(key:any) {
if (key === reactionval._id) {
if(isEmpty(data)){
//console.log('entro is empty', reactionval._id)
if (reactionval.count) {
data.push({"reactionId": key, "count": reactionval.count, "selected": selected})
}else{
data.push({"reactionId": key, "count": 0, "selected": selected})
}
}else{
for (valores of data) {
console.log('object values',Object.values(valores))
console.log('reactionval._id',reactionval._id)
if (Object.values(valores).indexOf(key) > -1) {
console.log('enter to exist', key)
}else{
console.log('enter to no exist', key)
if (reactionval.count) {
data.push({"reactionId": key, "count": reactionval.count, "selected": selected})
}else{
data.push({"reactionId": key, "count": 0, "selected": selected})
}
}
}
}
}
});
console.log('data',data)

此代码带有相应的值,这在我的 console.log 中,用于您看到的控制台的每个答案

key { _id: 'recommend', count: 3 }
key { _id: 'enjoy', count: 1 }
key { _id: 'contribute', count: 1 }
object values [ 'recommend', 3, true ]
reactionval._id enjoy
enter to no exist enjoy
object values [ 'enjoy', 1, false ]
reactionval._id enjoy
enter to exist enjoy
object values [ 'recommend', 3, true ]
reactionval._id contribute
enter to no exist contribute
object values [ 'enjoy', 1, false ]
reactionval._id contribute
enter to no exist contribute
object values [ 'contribute', 1, false ]
reactionval._id contribute
enter to exist contribute
object values [ 'contribute', 1, false ]
reactionval._id contribute
enter to exist contribute
data [
{ reactionId: 'recommend', count: 3, selected: true },
{ reactionId: 'enjoy', count: 1, selected: false },
{ reactionId: 'contribute', count: 1, selected: false },
{ reactionId: 'contribute', count: 1, selected: false }
]

如果你发现最后一个值是重复的,我不知道这行重复的是什么,我看到这是扩展,以及如何使这段代码更短

感谢您的回答

最佳答案

当循环开始贡献时,数据中有两个项目,下一行将不为真,因为这些项目是不同的项目:

if (Object.values(valores).indexOf(key) > -1)

这就是它重复最后一项的原因。如果你有更多的 react ,下一个 react 将增加三倍,依此类推。

我猜这个衬垫可以实现您想要实现的目标:

    const result = reactionsId.map(r => ({ reactionId: r, count: collection.filter(c => c._id === r).reduce((total, c) => total += c.count, 0), selected: collection1.some(c => c.reactionId === r) }));
console.log(result);

关于javascript - 两个对象和数组 foreach 将旧对象的值放入 typescript 中的新对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59255518/

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