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

227
Views
¿Cómo dividir una interfaz en varias matrices de claves separadas por tipo?

Dada una interfaz simple:

 interface IPerson { firstName: string; lastName: string; age: number; city: string; favoriteNumber: number; isMarried: boolean; hasDriverLicense: boolean; }

¿Cómo puedo generar matrices con nombres clave divididos por tipo? El resultado esperado sería así:

 ['firstName', 'lastName', 'city'] // string ['age', 'favoriteNumber'] // number ['isMarried', 'hasDriverLicense'] // boolean
about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

Si tiene una instancia de la interfaz y solo necesita usar los tipos disponibles en JS (por lo tanto, no las interfaces), es posible construir una estructura básica basada en los tipos de cada propiedad como esta:

 const example = { firstName: 'a', lastName: 'b', age: 1, city: 'c', favoriteNumber: 2, isMarried: true, hasDriverLicense: false, } const arraysOfTypes = (obj) => { // We'll build an object with types as keys const result = {} // Go through each property on the instance of the interface for (const [key, value] of Object.entries(obj)) { // Combine the existing keys and the new key into an array and add it to the result object result[typeof value] = [...(result[typeof value] || []), key] } console.log(result) } arraysOfTypes(example)

Esto podría expandirse con una verificación de tipos compleja dentro del ciclo for para detectar fechas/interfaces y luego pasar sus nombres a result[typeof value] en lugar de typeof value , sin embargo, eso es muy específico para su solución.

about 4 years ago · Santiago Gelvez 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!