How to count repeated values within a Javascript array

Asked

Viewed 2,440 times

1

I am learning in Rra Javascript, but I came across a situation that I could not solve. I’m looking to create a chart where The horizontal line will be the date (I’ve already managed to eliminate the repeating dates) and the vertical line the number of times it repeats, so I’d like to know how to count the dates that repeat in sequence and play within an array/vector. I’m using Firebase as my database.

Code: Function table() { ref = Fire_input()

var coluna = []

ref.child("Tabela").on("child_added", function (snapshot) {
    dadosT = snapshot.key

    ref.child("Tabela").child(dadosT).on("value", function (snapshot) {

        dados = snapshot.val(); // Array completo 


        //CRIA O GRÁFICO

        coluna.push(dados.Data) // Array com todas as datas

        // ELIMINA AS DATAS REPETIDAS
        var novaColuna = coluna.filter(function (este, i) {
            return coluna.indexOf(este) === i;
        });

        // SOMA AS DATAS REPETIDAS
                ??????

 });

});

}

  • Your question is not clear. For example: do you want the count of repeated values in sequence, or the totality of repetitions? How will you know what word the returned array is referring to? Wouldn’t it be better to return an object (hashtable/dictionary)? Elaborate better.

  • Repeaters will always be in sequence?

  • Object.defineProperties(Array.prototype, { Count: { value: Function(value) { Return this.filter(x => x==value).length; } } }); arr.Count("John") expected result: 4

  • for me no matter if it will bring in sequence only count the equal values that repeat, but if bring in better sequence even, thank you.

  • @Jeffsonoliveira, have you at least tried to solve this problem? Please click [Edit] to add the code you tried to do...

  • If the array is ["João", "Maria", "José", "João"] what must be the return? [2, 1, 1]? Or does John only count twice if he has more than one occurrence in a row? I think that’s what they meant when they asked if the repeaters are in sequence.

  • Blz will edit my question by entering my code.

Show 2 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.