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

132
Visualizações
How to use in Angular inside an arrow function an object property (deeper than 1st level) sent by input?

I'm using angular 7 and trying to do something in a child component : use the input property that can be at first level in the object or deeper.

My child component has this piece of code :

if (this.values.filter(obj => obj[this.matchPropertyName] === $event[i].id).length === 0) {
  ...
}

where this.matchPropertyName is my input (can be 'id', 'myProperty.id',...)

For a single level (obj.id) this code works. However, I need to use sometimes from a deeper level (obj.myProperty.id) and it doesn't work. How can I achieve this ?

Let me know if it not clear enough.

I'm using angular 7 and typescript 3.2.4

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

0

I don't think there's a built-in solution but you can make use of a simple split and reduce. For example:

const value = this.matchPropertyName.split('.').reduce((pre, curr) => pre[curr], obj);

would give the value for obj.myProperty.id when this.matchPropertyName="myProperty.id"

Stackblitz

So in your case, you could use it like:

const theValue = this.matchPropertyName.split('.').reduce((pre, curr) => pre[curr], obj);
if (this.values.filter(obj => theValue === $event[i].id).length === 0) {
  ...
}

Final result by the OP:

myEventListener($event) {
   if (this.values.filter(obj => this.resolveProperty(obj) === $event[i].id).length === 0) { 
      ... 
   }
}  
  
resolveProperty(obj) {
   return this.matchPropertyName.split('.').reduce((pre, curr) => pre[curr], obj);   
}
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