How to remove duplicate values from a multidimensional array in javascript?

5

Good evening,

I need to remove duplicate results in an array with other nested arrays.

I have the following variable:

 var dados = [
  ['maria', 'telefone', '324-5678', true],
  ['maria', 'telefone', '91234-5555', true],
  ['maria', 'telefone', '324-5678', false],
  ['jonas', 'telefone', '98888-1111', true],
  ['jonas', 'telefone', '56789-1010', true],
];

Where all records of the same name and with the same phone value should be removed.

In this case, the value to return should be:

 var dados = [
  ['maria', 'telefone', '91234-5555', true],
  ['maria', 'telefone', '324-5678', false],
  ['jonas', 'telefone', '98888-1111', true],
  ['jonas', 'telefone', '56789-1010', true],
];

I tried to use the set, but it only removes duplicate values if all values are in a single array.

Thank you for your attention.

    
asked by anonymous 21.05.2018 / 04:57

3 answers

5

The semantic method to use is .filter to filter the main array and .some to check for copies. The advantage of using .some is to be an iterator that stops when it encounters a case that gives true , saving processing of the rest of the array.

You have to compare, as you indicated, that the name and phone are the same.

You can do it like this:

var dados = [
  ['maria', 'telefone', '324-5678', true],
  ['maria', 'telefone', '91234-5555', true],
  ['maria', 'telefone', '324-5678', false],
  ['jonas', 'telefone', '98888-1111', true],
  ['jonas', 'telefone', '56789-1010', true],
];

var filtrados = dados.filter(([nome, _, telefone], i) => {
  return !dados.some(
    ([_nome, __, _telefone], j) => j > i && nome === _nome && telefone === _telefone
  );
});

console.log(filtrados);
    
21.05.2018 / 07:26
2

You can move through the array and for each position go through the array again verifying duplicity. If it does not find duplicity add the value in a new array.

var dados = [
  ['maria', 'telefone', '324-5678', true],
  ['maria', 'telefone', '91234-5555', true],
  ['maria', 'telefone', '324-5678', false],
  ['jonas', 'telefone', '98888-1111', true],
  ['jonas', 'telefone', '56789-1010', true],
];

var novaLista = [];

dados.forEach(function(item1, x) {
  var duplicado = false;
  dados.forEach(function(item2, y) {
    if (x < y) {
      if (item1[0] == item2[0] && item1[2] == item2[2]) {
        duplicado = true;
        return false;
      }
    }
  });

  if (!duplicado) {
    novaLista.push(item1);
  }
});

console.log(novaLista);
    
21.05.2018 / 05:50
2

You can use the filter method and do the verification.

var dados = [
  ['maria', 'telefone', '324-5678', true],
  ['maria', 'telefone', '91234-5555', true],
  ['maria', 'telefone', '324-5678', false],
  ['jonas', 'telefone', '98888-1111', true],
  ['jonas', 'telefone', '56789-1010', true],
];

dados = dados.filter((item, pos, array) => {
  return array.map(x => x[2]).indexOf(item[2]) === pos;
});

console.log(JSON.stringify(dados));

x[2] and item[2] is the index of the phone in the array.

References

21.05.2018 / 06:01