Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

142
Views
¿Cómo filtrar un valor de bye de matriz múltiple [0] de cada línea?

Me gustaría filtrar una matriz múltiple en JS por el valor [0] de cada línea. En mi próxima matriz, hay varios "visiteur", "paris", "monaco" y quiero tener solo uno de cada uno. Alguna idea ?

Mi matriz:

0: ['nombre', 'id']

1: ['Equipo de FootContact', '1']

2: ['Club de fútbol Cergy Pontoise', '2']

3: ['visitante', '42']

4: ['visitante', '43']

5: ['visitante', '44']

6: ['mónaco', '45']

7: ['mónaco', '46']

8: ['parís', '47']

9: ['parís', '48']

Espero esto:

0: ['nombre', 'id']

1: ['Equipo de FootContact', '1']

2: ['Club de fútbol Cergy Pontoise', '2']

3: ['visitante', '42']

6: ['mónaco', '45']

8: ['parís', '47']

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

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 Report

0

En términos generales, desea mantener un elemento en su matriz si no apareció de antemano.

  • input.filter((element, index, array) => ...) utiliza los argumentos de filtro adicionales
  • array.slice(0, index) contiene los elementos de la matriz más pequeños que el índice actual
  • some(element2 => (element[0] === element2[0])) devuelve verdadero si al menos un elemento anterior es idéntico wrt. al primer elemento
 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!