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

157
Views
react pass object key as prop

I want to pass array of strings from parent function component to child function component. At the child component the array should reflect the object key.

For example, send the following array from a parent BY PROP: let colors = ['red','green','white']

and at the child component I have the following variable:

let arr = [config.red, config.green, config.white, config.blue]; 

I want to take the ".red, .green, .white" from the props and then go over the config variable below:

const config: {
red: {
    base: string;
    icon: any;
    name: string;
    target: string;
};
white: {
    base: number;
    icon: any;
    name: string;
    target: string;

...

Be very glad for any help. Best.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Pass the array of keys to your component...

<Child colors={colors} />

then reduce that array to the matching keys from config

const arr = props.colors.reduce(
  (acc, key) => (key in config ? [...acc, config[key]] : acc),
  []
);

I'm using reduce() instead of a simple map() in order to filter out keys that might not be in config.

about 4 years ago · Juan Pablo Isaza Report

0

<Child colors={["red", "green", "white"]} />

and inside Child, you can get the respective config object, using map and iterating over it

const arr = props.colors.map((item,index)=>{
      
       return config[item];
})
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!