Javascript 两个对象和数组foreach在typescript中将值从旧对象放到新对象
我有两个对象和一个数组Javascript 两个对象和数组foreach在typescript中将值从旧对象放到新对象,javascript,arrays,typescript,object,loopback4,Javascript,Arrays,Typescript,Object,Loopback4,我有两个对象和一个数组 var collection = [ { _id: 'recommend', count: 3 }, { _id: 'enjoy', count: 1 }, { _id: 'contribute', count: 1 } ] var collection1 = [ { _id: 'ZWx0aWVtcG86MTIzNDU6ZXNwYW5nbGlz', websiteId: 'eltiempo', sectionSl
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']
我最初的想法是为每个ReactionId从集合中获取值,如果存在则放入计数,如果不存在则放入计数或计数0,如果collection1中存在值,则选择true;如果不存在,则选择put 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)
这段代码带有相应的值,对于您看到的控制台的每个答案,这都会出现在my 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 }
]
如果你认为最后一个值是duplicate,我不知道这行是什么duplicate,我看到这是extend,如何使代码更简短
感谢您的回答当循环开始贡献时,数据中有两项,下一行将不为真,因为这些项是不同的项:
if (Object.values(valores).indexOf(key) > -1)
这就是它复制最后一项的原因。如果您有更多的ReactionID,那么下一个将增加三倍,等等
我想,这一行可以实现您想要实现的目标:
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);
TypeScript部分在哪里?仅声明变量