Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

188
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda