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

181
Vistas
Cannot make intersection between two nested arrays

I'm making translations dynamic, but I'm struggling in the part when I have to show the corresponding object from the other array.

I have two arrays, which have the same model, and what I'm trying to do is, when I search for a translation, I want to show also the corresponding translation from the other language.

For the moment, I can only show the one that you are searching for.

For example: If I search for the sentence Are you sure?, besides the Are you sure? sentence, it should show also the corresponding sentence from the other column language.

Here is a snippet with an example: https://codesandbox.io/s/polished-smoke-n6w6i?file=/src/App.js

Any help would be very appreciated. Thank you in advance!

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

The logic is to filter the selected language items by their id, not by their value. The id is taken from the default language items.

This is how

const searchByresourceId = (targetLng, selectedResources) => {
  // get the default language item id
  const ids = selectedResources.map(({languageResources}) => languageResources.map(({id}) => id)).flat();

  return targetLng
    ?.map((item) => {
      const languageResources = item.languageResources.filter(
        (namespace) =>
          ids.includes(namespace.id)
      );
      return {
        ...item,
        languageResources
      };
    })
    .filter((item) => item.languageResources.length > 0);
}

Ans you should change how you set the state

const searchByresourceId = (targetLng, selectedResources) => {
  const ids = selectedResources.map(({languageResources}) => languageResources.map(({id}) => id)).flat();
  return targetLng
    ?.map((item) => {
      const languageResources = item.languageResources.filter(
        (namespace) =>
          ids.includes(namespace.id)
      );
      return {
        ...item,
        languageResources
      };
    })
    .filter((item) => item.languageResources.length > 0);
}

https://codesandbox.io/s/cranky-ellis-ccdhx?file=/src/App.js

The code is not optimized of course, I just demonstrated the logic

over 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