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

261
Views
¿Cómo usar en Angular dentro de una función de flecha una propiedad de objeto (más profunda que el primer nivel) enviada por entrada?

Estoy usando angular 7 e intento hacer algo en un componente secundario: use la propiedad de entrada que puede estar en el primer nivel en el objeto o más profundo.

Mi componente hijo tiene este fragmento de código:

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

donde this.matchPropertyName es mi entrada (puede ser 'id', 'myProperty.id',...)

Para un solo nivel (obj.id) este código funciona. Sin embargo, a veces necesito usar desde un nivel más profundo (obj.myProperty.id) y no funciona. Cómo puedo conseguir esto ?

Déjame saber si no está lo suficientemente claro.

estoy usando angular 7 y mecanografiado 3.2.4

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

No creo que haya una solución integrada, pero puede utilizar una simple split y reduce . Por ejemplo:

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

daría el valor para obj.myProperty.id cuando this.matchPropertyName="myProperty.id"

apilar

Entonces, en tu caso, podrías usarlo como:

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

Resultado final por el 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); }
over 4 years ago · Santiago Trujillo 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!