-2
Good afternoon
this script only works if my Array is always sorter from oldest to newest date
How would you give
var dados = [
["09/01/2020","JOAO"],
["13/01/2020","PAULO"],
["23/01/2020","JOAO"],
["01/01/2020","JOAO"],
["01/01/2020","PAULO"],
["02/01/2020","JOAO"],
["02/01/2020","PAULO"],
["03/01/2020","JOAO"]
]
var novo_dados = []
ar = []
function addArr(el,str){
if(!el.includes(str)){
el.push(str)
return true
}
}
for(i=dados.length-1;i>0;i--){
nome = dados[i][1]
if(addArr(ar,nome)){
novo_dados.push(dados[i])
}
}
console.log(novo_dados)
The method
sort
waits for a numerical return (negative for less, equal to 0 for equivalent, positive for greater). You are returning a boolean.– Andre
It is true @user140828, but remember that there is an implicit conversion where
+true === 1
and+false === 0
are true. I will make a change to consider the-1
– Marcelo Vismari
You don’t need all these
if
andelse
, just doa[0]
-b[0]
, because objects of the typeDate
implement the methodvalueOf
to return a numerical value (the number of milliseconds since Unix Timestamp).– Andre