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

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

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

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 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