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

359
Visualizações
Extract string from object and pass it to parent function - Typescript , React

I am trying to achieve something rather simple, but i do not know why i am so stuck on it. I want to send data to an parent function. This data is only acceptable in a form of a string. But my issue here is that sometimes on click event i may have multiple strings The final goals is to send them both up

setNewFruits('banana' , 'mango')

So on click i am getting my object

...
  const foundFruits = foundFruitsCategory.fruits

console.log : {name: 'banana', id: 1, color: 'yellow'}

And the if i want to extract the name only i do :

 const foundAndSelectedFruit = foundFruits.map((fruit) => fruit.name)

console.log output:

['banana']

But the thing is i want to have the name only as a string banana and not as array of strings. Yes i can say

console.log(foundAndSelectedFruit[0]) 

That will give me the banana , but the thing is sometimes onClick i could have two objects

console.log output:

[
  {name: 'banana', id: 1, color: 'yellow'}
  {name: 'mango', id: 2, color: 'orange'}
]

So the log of foundAndSelectedFruit will be

['banana','mango']

How i can extract them as seperate strings and send them to my update function ? ( The function awaits a string of name )

P.S. I already tried with forEach loop , but instead of printing my string it gives me back undefined

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Have you tried the spread operator?

setNewFruits(...foundAndSelectedFruit)
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