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

174
Views
¿Cómo puedo encontrar un valor específico dentro de una matriz anidada?

Pregunta: Tengo esta matriz que tiene una matriz anidada que quiero recorrer para encontrar un valor específico.

 arr = [['123456','234567'], ['345678']]; specificValue = '123456';

El resultado que quiero es averiguar si hay un valor igual a un valor específico y devolver este valor.

Lo intenté

 arr.filter(id => id === specificValue);

Gracias por la ayuda

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

0

Prueba este código

 const arr = [['123456','234567'], ['345678']]; const result = arr.flat(Infinity).find(val => val === "123456"); console.log(result);

Puede obtener más información sobre el método array.flat() aquí https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat

about 4 years ago · Juan Pablo Isaza Report

0

Sigamos adelante con tu intento.

El problema es que su matriz está anidada, por lo que array.filter no funcionará

Use array.flat con array.filter :

 let arr = [['123456','234567'], ['345678']]; let specificValue = '123456'; console.log(arr.flat().filter(i=>i==specificValue))

about 4 years ago · Juan Pablo Isaza Report

0

Dado que flat () toma una profundidad como argumento, para poder buscar en un número indefinido de matrices anidadas, puede probar la buena recursividad:

 const findRecursive = (array, target) => { for (let i = 0; i < array.length; i++) { if (array[i] === target) { return array[i]; } else if (Array.isArray(array[i])) { return findRecursive(array[i], target); } } } console.log(findRecursive(arr, specificValue));
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!