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

Asked

Viewed 2,286 times

5

Good night,

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 be returned 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 using the set, but it only removes duplicated values if all values are in a single array.

Thank you for your attention

3 answers

5


The semantic method to be used is the .filter to filter the main array and the .some to check for copies. The advantage of using the .some is to be an iterator who stops when he finds a case that works true, saving the processing of the rest of the array.

You have to compare, as you pointed out, that the name and the 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);

  • Thanks @Sergio, it worked perfectly.

3

You can use the method filter and make the check.

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 phone index in the array.

References

2

You can go through the array and for each position go through it again by checking the duplicity. If you can’t 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);

Browser other questions tagged

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