Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

134
Visualizações
How to filter a multiple array bye value[0] of each line?

I would like to filter a multiple array in JS by the value[0] of each line. In my next array, there are several "visiteur", "paris", "monaco" and I want to have juste one of each. Any idea ?

My array :

0: ['firstname', 'id']

1: ['FootContact Team', '1']

2: ['Cergy Pontoise Football Club', '2']

3: ['visiteur', '42']

4: ['visiteur', '43']

5: ['visiteur', '44']

6: ['monaco', '45']

7: ['monaco', '46']

8: ['paris', '47']

9: ['paris', '48']

I expect this :

0: ['firstname', 'id']

1: ['FootContact Team', '1']

2: ['Cergy Pontoise Football Club', '2']

3: ['visiteur', '42']

6: ['monaco', '45']

8: ['paris', '47']

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

let listOfUsers = [['toto', 1], ['titi', 2],['titi', 5], ['visiteur',3], ['visiteur', 4]];
let filtered = {};
listOfUsers = listOfUsers.filter((user) => {
    let name = user[0];
    if (!filtered[name]) {
        // set flag and return true since it's the first happening
        return filtered[name] = true;
    }
});
console.log(listOfUsers);
about 4 years ago · Juan Pablo Isaza Relatório

0

Generically speaking, you want to keep an element in your array if it did not show up beforehand.

  • input.filter((element, index, array) => ...) uses the additional filter arguments
  • array.slice(0, index) contains the array elements smaller than the current index
  • some(element2 => (element[0] === element2[0])) returns true if at least one prior element is identical wrt. to the first element

let input = [
    ['firstname', 'id'],
    ['FootContact Team', '1'],
    ['Cergy Pontoise Football Club', '2'],
    ['visiteur', '42'],
    ['visiteur', '43'],
    ['visiteur', '44'],
    ['monaco', '45'],
    ['monaco', '46'],
    ['paris', '47'],
    ['paris', '48'],
]

let expectedOutput = [
    ['firstname', 'id'],
    ['FootContact Team', '1'],
    ['Cergy Pontoise Football Club', '2'],
    ['visiteur', '42'],
    ['monaco', '45'],
    ['paris', '47'],
]

let actualOutput = input.filter((element, index, array) =>  !array.slice(0, index).some(element2 => (element[0] === element2[0])))

console.log(
  JSON.stringify(actualOutput) 
  ===
  JSON.stringify(expectedOutput)
)
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda