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

98
Views
Devolver una propiedad particular de un objeto de una matriz de objetos

Mi matriz de objetos se parece a esto

 const arr1 = [ {person: {isPresent: true, isInsured: true, value:20}}, {status: {isPresent: true, isInsured: true, value:20}}, ]

Quiero devolver un objeto/matriz que se parezca a esto

 [ {person: {value:20}}, {status: {value:20}}, ]

Intenté mapear esta matriz pero no puedo obtener el resultado deseado y me pregunto qué me estoy perdiendo. Cualquier ayuda apreciada, gracias!

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

0

Es posible que necesite algo como esto:

 interface Details { isPresent: boolean; isInsured: boolean; value: number; } interface Person { person: Details; } interface Status { status: Details; } type Tuple = [Person, Status]; const arr1: Tuple = [ { person: { isPresent: true, isInsured: true, value: 20 } }, { status: { isPresent: true, isInsured: true, value: 20 } }, ]; const extractValue = ({ value }: Details) => ({ value }); const result = arr1.map(x => { if ('person' in x) { return { person: extractValue(x.person) }; } if ('status' in x) { return { status: extractValue(x.status) }; } return x; }); console.log(result);

Puedes jugar con él en este patio de juegos de TypeScript

about 4 years ago · Juan Pablo Isaza Report

0

Puedes hacerlo:

 const arr1 = [ {person: {isPresent: true, isInsured: true, value:20}}, {status: {isPresent: true, isInsured: true, value:20}}, ]; const newArray = []; for (const val in arr1) { const keys = Object.keys(arr1[val]); keys.forEach((key, index) => { //console.log(key); //console.log(arr1[val][key]['value']); newArray.push({[key]: {value: arr1[val][key]['value']}}); }); } console.log(newArray);

Volverá:

 [ { person: { value: 20 } }, { status: { value: 20 } } ]
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!