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

190
Vistas
React map - returning value

how can I return just one element from an array from another file where element (let say it's "2") is equal to id in that file

import { AnotherFile } from './AnotherFile'

const element = exFunc(element)

const exFunc = (val) => {
      {AnotherFile.map((data) =>
         data.id === parseInt(val) &&
         return {data}
      )} 
   }

This is the other file with data:

export const AnotherFile = [
   {
      id: 1,
      text: 'Num 1',
   },
   {
      id: 2,
      text: 'Num 2',
   },
   {
      id: 3,
      text: 'Num 3',
   }
]

Ps. More exactly i need a "text" value

alert("The text is: " + <>text value of data with id=2 from AnotherFile<>)
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

It doesn't work with && like that in the function, you would need to do a proper logic block:

const exFunc = (val) => {
  AnotherFile.map((data) => {
    if (data.id === parseInt(val)) return { data };
  });
};
about 4 years ago · Juan Pablo Isaza Denunciar

0

You can try to use the filter method for this. Please note the "?" operator to be sure that you don't try to access an item that does not exist

const exFunc = (val) => AnotherFile.filter((v) => v.id === val)[0]?.text;

console.log(exFunc(2));
about 4 years ago · Juan Pablo Isaza Denunciar

0

This part :

data.id === parseInt(val) &&
         return {data}

don't work inside a function

Try this :

if(data.id === parseInt(val)) return {data}
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