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

202
Vistas
Get index of parent given a value of a child array of arrays

I'm trying to get the index of the parent given an element inside the children, for example, I have a list variable (2) [Array(1), Array(2)] with this structure:

[
    [
        {id: "1",contactType: {id: "phoneNumber",company: {id: "01",name: "Company01"}},value: "5555555555"},
    ],
    [
        {id: "2",contactType: {id: "phoneNumber",company: {id: "03",name: "Company03"}},value: "7777777777"},
        {id: "3",contactType: {id: "phoneNumber",company: {id: "05",name: "Company05"}},value: "8888888888"},
    ],
]

I tried using includes and findIndex to verify if such element exist first and then get the parent index:

list.includes('5555555555', 0);

I expected to get true because I asked include to begin searching in index 0 of the list exactly where "5555555555" element is, but i got false instead.

Also tried with:

list.findIndex(x => x.value === '5555555555');

I expected 0 since element 5555555555 is in parent 0 index. But got -1 instead.

I also tried using flat() to get into the children and the use includes, but then I lose the 0 and 1 index of the original list.

Expected output:

foo(list,'7777777777'); should return 1 since that element is inside the second array of the list or foo(list,'8888888888'); should also return 1 since that element is also inside the second array of the list.

Can someone guide me on how to approach this issue?

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

0

Use Array.findIndex with condition

const data = [
[
    {id: "1",contactType: {id: "phoneNumber",company: {id: "01",name: "Company01"}},value: "5555555555"},
],
[
    {id: "2",contactType: {id: "phoneNumber",company: {id: "03",name: "Company03"}},value: "7777777777"},
    {id: "3",contactType: {id: "phoneNumber",company: {id: "05",name: "Company05"}},value: "8888888888"},
],
];

const index = data.findIndex(item => item.findIndex(node => node.value === "5555555555") > -1);
console.log(index)

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