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

193
Vistas
Intersección de dos matrices en javascript

Quiero buscar en una matriz grande una identificación diferente de otra matriz e imprimir todas las intersecciones de esas dos matrices

Quiero mapear a través de mi bigTable y quiero crear otra matriz de correspondencia, cada elemento encontrado debe contener todos los campos + tableName + tableID como este:

 const output = [{ ID: 1234, title: 'title1', TableName: 'loramIpsum', TableId: 11, }, { ID: 98523, title: 'mylasttitle', TableName: 'table2', TableId: 87545, }, { ID: 97766, title: 'mylastdata', TableName: 'table2', TableId: 87545, }, ]

Creé una función, pero creo que hay otra solución mejor y de muestra, esta es mi función:

 const getResult = (wantedData, bigArray) => { return wantedData.flatMap((id) => bigArray.flatMap((family) => family.Tables.flatMap((table) => { let item = table.myDatas.find((el) => el.ID === id); if (item) { item.Table = table.TableName; item.familyId = family.GridId; return item; } }).filter((result) => result !== undefined) ) ); }; console.log(getResult(wantedData, bigArray))
 <script> const wantedData = [1235, 98523, 97766]; const bigArray = [{ bigArrayId: 1111, Tables: [{ TableId: 11, TableName: 'loramIpsum', myDatas: [{ ID: 1234, title: 'title1', }, { ID: 1235, title: 'title2', }, ], }, ], }, { bigArrayId: 674665, Tables: [{ TableId: 87545, TableName: 'table2', myDatas: [{ ID: 98523, title: 'mylasttitle', }, { ID: 24134, title: 'alex', }, { ID: 97766, title: 'mylastdata', }, ], }, ], }, ]; </script>

¿Alguna ayuda por favor? ¿Puedo hacerlo con la función recursiva?

about 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

Creo que necesitas resolver este problema en dos pasos:

  • Primero, cree una matriz plana de tablas
  • Luego filtre la matriz por condiciones

 const bigArray=[{bigArrayId:1111,Tables:[{TableId:11,TableName:"loramIpsum",myDatas:[{ID:1234,title:"title1"},{ID:1235,title:"title2"}]}]},{bigArrayId:674665,Tables:[{TableId:87545,TableName:"table2",myDatas:[{ID:98523,title:"mylasttitle"},{ID:24134,title:"alex"},{ID:97766,title:"mylastdata"}]}]}]; const wantedData = [1235, 98523, 97766]; const flatTables = bigArray.flatMap(({ Tables }) => Tables.flatMap(({ myDatas, TableId, TableName }) => myDatas.map((data) => ({ ...data, TableId, TableName })) )); const result = flatTables.filter(({ ID }) => wantedData.includes(ID)); console.log(result);
 .as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Santiago Trujillo Denunciar

0

Creo que si hace algo como esto, podrá mapear después de haber encontrado las cosas correctas en lugar de mapear todo y luego filtrar ese resultado:

 const bigArray = [{ bigArrayId: 1111, Tables: [{ TableId: 11, TableName: 'loramIpsum', myDatas: [{ ID: 1234, title: 'title1', }, { ID: 1235, title: 'title2', }, ], }, ], }, { bigArrayId: 674665, Tables: [{ TableId: 87545, TableName: 'table2', myDatas: [{ ID: 98523, title: 'mylasttitle', }, { ID: 24134, title: 'alex', }, { ID: 97766, title: 'mylastdata', }, ], }, ], }, ]; const wantedData = [1235, 98523, 97766]; const wanted_set = new Set(wantedData); const push_concat = (arr1, arr2) => { for(let i = 0; i < arr2.length; i++) arr1.push(arr2[i]); return arr1; }; const res = bigArray.reduce( (acc, { Tables }) => push_concat(acc, Tables.flatMap(({ TableId, TableName, myDatas }) => myDatas .filter(({ ID }) => wanted_set.has(ID)) .map(({ ID, title }) => ({ ID, title, TableId, TableName })) ) ), [] ); console.log(res);

about 4 years ago · Santiago Trujillo 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