0

I have two object and a 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']

My initial idea is for each reactionsId get values from collection and put the count if exist put count or count 0 if no,and select true if a value exist in collection1 or put false in case that no, the final result will be, but my code belows i have problem

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

this is my code

  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)

and this code with the correspond values and this ins my console.log for each answer of console that you see

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 you se the last value is duplicate i dont know what comes to this line duplicate, and i see this is extend, and how can to make this code more short

thanks for your answers

jonathan
  • 114
  • 9

1 Answers1

1

when the loop gets to contribute you have two items in data and the next line won't be true since these items are different items:

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

this is why it duplicates the last item. in case you would have more reactionids the next will be tripled, etc..

this one liner does what you want to achieve, i guess:

    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);
tano
  • 2,657
  • 1
  • 15
  • 16