Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

176
Vistas
How can I find a specific value inside a nested array?

Question: I have this array that have a nested array that I want to loop to find a specific value.

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

The output that I want is to find out if there's a value same with specificvalue and return this value?

I tried

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

Thanks for the help

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Try this code

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

You can learn more about array.flat() method here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat

about 4 years ago · Juan Pablo Isaza Denunciar

0

Let's keep going with your attempt.

The problem is your array is nested so array.filter will not work

Use array.flat with 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 Denunciar

0

Since flat() takes a depth as an argument, to be able to search in an indefinite number of nested arrays you could try good old recursion:

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda