两个对象和数组 foreach 将旧对象的值放入打字稿中的新对象
two objects and array foreach put the values from old object to new object in typescript
我有两个对象和一个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,并且 select 如果值存在于 collection1 中则为真,如果不存在则为假,最终结果会是,但是我下面的代码有问题
[
{
"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)
这就是它重复最后一项的原因。如果你有更多的 reactionids,下一个将增加三倍,等等。
我猜这一个班轮可以达到你想要的效果:
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);
我有两个对象和一个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,并且 select 如果值存在于 collection1 中则为真,如果不存在则为假,最终结果会是,但是我下面的代码有问题
[
{
"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)
这就是它重复最后一项的原因。如果你有更多的 reactionids,下一个将增加三倍,等等。
我猜这一个班轮可以达到你想要的效果:
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);