3
I would like to know how to return the amount of times each object appeared in an array, for example:
Entree:
array1 = [
{nome: joao},
{nome: maria},
{nome: joao},
{nome: carlos},
{nome: joao},
{nome: carlos},
]
Exit:
array2 = [
{nome: joao, quantidade: 3},
{nome: maria, quantidade: 1},
{nome: carlos, quantidade: 2}
]
My code:
for(var h in array1){
array3.push({
[array1[h].nome]: false
})
}
for(var i in array1){
var count = 1
var nome = array1[i].nome
for(var k in array3){
if(array3[k][nome]==false){
for(var j=i+1; j<array1.length; j++){
if(array1[i].nome==array1[j].nome){
count++
total+=count
array3[k][nome] = true
}
}
array2.push({
nome: nome,
quantidade: count
})
break
}
}
}
Excellent answer, I always try to answer in the simplest possible way, because I do not know for sure the knowledge on the subject of who asked the question, but, your answer demonstrates a great knowledge in language. + 1.
– LeAndrade