0

I know this question has been asked before, but I couldn't find a solution to this ... I'm simply doing this:

 var tagsToUse = [];
 var refTags = firebase.database().ref('/tag/');
        refTags.once('value', function(tags){
          pois.forEach(p=>{
            tags.forEach(function(t){
              if(p.tag == t.key){
                 console.log("Index " + tagsToUse.indexOf(t.child('name').val()))
                 if(tagsToUse.indexOf(t.child('name').val())==-1){o
                    tagsToUse.push(t.child('name').val())
                    console.log("Added "+ t.child('name').val())
                   }
               }
              return false;
            })

          })

        })

It keeps adding duplicates and the Index print is always -1, even if the name is already in the array. At the end I have an array with duplicates. What is wrong with that? Why is it printing -1?

Usr
  • 2,628
  • 10
  • 51
  • 91
  • Specify the array content of `tagsToUse` so that it will be easy to debug your code. – Ankit Agarwal Jan 20 '18 at 12:05
  • 1
    It's really hard to say an answer without seeing the structure of your objects. Pay attention that `indexOf` uses the strict equality, so if you have an array of numbers for the tags keys, and getting the `val` you will have a string, this won't match – quirimmo Jan 20 '18 at 12:11

0 Answers0