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

75
Vistas
search an item in multidimentionnal array

I'm trying to find an element on a multidimentionnal array usin JAVASCRIPT function, but I get error

This is my array's data:

export const datas = [
    {
        id: 1,
        firstName: 'John',
        tables: [
            { ID: 11, title: 'Lorem' },
            { ID: 12, title: 'Ipsum' },
        ],
    },
    {
        id: 2,
        firstName: 'Doe',
        tables: [
            {
                ID: 22,
                title: 'Arke',
                nodes: [{ name: 'Name1' }, { name: 'Name2' }, { name: 'Name3' }],
            },
            { ID: 23, title: 'Korem' },
        ],
    },
    {
        id: 3,
        firstName: 'Brad',
        tables: [
            {
                ID: 30,
                title: 'Mern',
                nodes: [{ name: 'Name4' }, { name: 'Name5' }, { name: 'Name6' }],
            },
            {
                ID: 31,
                title: 'Full',
                nodes: [{ name: 'Name7' }, { name: 'Name8' }, { name: 'Name9' }],
            },
        ],
    },
];

I've tried a reccursive function but it's not work, this is my code :

export const findById = (arr, id) => {
    for (let o of arr) {
        if (o.tables.length > 0) {
            let a = findById(o.tables.nodes, 'id');
            console.log(a);
        }
    }
};

I want to print the Object with ID 22, the problem is that I don't have the same structure in each dimension, and it still confuse me..

My Input : 22

My output :

{
    ID: 22,
    title: 'Arke',
    nodes: [{ name: 'Name1' }, { name: 'Name2' }, { name: 'Name3' }],
},

Have you an idea how to edit my function to get my input's response ?

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

0

Your recursive function wasn't too far off, you need to check if the item as a tables first before recursively calling it again. And then finally just check the ID in the loop.

eg..

const datas=[{id:1,firstName:"John",tables:[{ID:11,title:"Lorem"},{ID:12,title:"Ipsum"}]},{id:2,firstName:"Doe",tables:[{ID:22,title:"Arke",nodes:[{name:"Name1"},{name:"Name2"},{name:"Name3"}]},{ID:23,title:"Korem"}]},{id:3,firstName:"Brad",tables:[{ID:30,title:"Mern",nodes:[{name:"Name4"},{name:"Name5"},{name:"Name6"}]},{ID:31,title:"Full",nodes:[{name:"Name7"},{name:"Name8"},{name:"Name9"}]}]}];


function findById(arr, ID) {
  for (const a of arr) {
    if (a.tables) {
      const r = findById(a.tables, ID);
      if (r) return r;
    }
    if (a.ID === ID) return a;
  }
}

console.log(findById(datas, 22));

about 4 years ago · Juan Pablo Isaza Denunciar

0

if you just need the nested data you can use flatMap and find

const findById = (arr, id) =>
  arr
  .flatMap(d => d.tables)
  .find(t => t.ID === id)



const datas = [{
    id: 1,
    firstName: 'John',
    tables: [{
        ID: 11,
        title: 'Lorem'
      },
      {
        ID: 12,
        title: 'Ipsum'
      },
    ],
  },
  {
    id: 2,
    firstName: 'Doe',
    tables: [{
        ID: 22,
        title: 'Arke',
        nodes: [{
          name: 'Name1'
        }, {
          name: 'Name2'
        }, {
          name: 'Name3'
        }],
      },
      {
        ID: 23,
        title: 'Korem'
      },
    ],
  },
  {
    id: 3,
    firstName: 'Brad',
    tables: [{
        ID: 30,
        title: 'Mern',
        nodes: [{
          name: 'Name4'
        }, {
          name: 'Name5'
        }, {
          name: 'Name6'
        }],
      },
      {
        ID: 31,
        title: 'Full',
        nodes: [{
          name: 'Name7'
        }, {
          name: 'Name8'
        }, {
          name: 'Name9'
        }],
      },
    ],
  },
];

console.log(findById(datas, 22))

about 4 years ago · Juan Pablo Isaza Denunciar

0

js has amazing array options https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array

the ones which will help you most are probably:

  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flatMap

here are some examples

// get the base with id 22
const baseWith22ID = datas.filter(f => f.tables.filter(s => s.id = 22))
// (i guess you want this one) get all elements with id 22
const onlyElementsWith22ID = datas.flatMap(f => f.tables.filter(s => s.id = 22))
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