Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

354
Views
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 answers
Answer question

0

Have you tried the spread operator?

setNewFruits(...foundAndSelectedFruit)
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!