Remove Multidimensional javascript array duplicity

Asked

Viewed 161 times

0

I don’t know how to do not include duplicates, or remove duplicates after added, can help me please?

Obs: I checked other codes I found on stakoverflow and other sites, but they didn’t work for me.

agendaOff.length = 2

agendaOff[0][0] = 1
agendaOff[0][1] = "17/04/2018 20:25:40"

agendaOff[1][0] = 1
agendaOff[1][1] = "17/04/2018 20:25:40"

// Método que utilizo para adicionar itens no array:
$.getJSON('json/json_query_requisicao.asp',function(data){
    if(data.length > 0){ v1='';v2='';v3='';var opt ='';
        $.each(data,function(w,itens){
            v1 = data[w].id_tiporequisicao;
            v2 = data[w].datahora;
            agendaOff.push([v1,v2]);
        });
    }
}); 

1 answer

1


There is no extremely practical way to compare objects/arrays. If you wanted an array with unique values of numbers, strings, or another primitive, just use new Set(agendaOff). But how agendaOff has arrays, you have to use a function to compare these array.

A commonly used method is to use filter + indexOf to search for an item, and compare its position with the one found. If the position of the item is different from the one found, it means another item with the same value, so it is a duplicate.

$.getJSON('json/json_query_requisicao.asp', function(data) {
    agendaOff = agendaOff
        .concat(data.map((item) => [item.id_tiporequisicao, item.datahora]))
        .filter((item, i, self) => self.findIndex((itemF) => item[0] === itemF[0] && item[1] === itemF[1]) === i);
});
  • It worked perfectly Thank you, I had built something more similar without the filter, vlw thank you.

Browser other questions tagged

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