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

164
Views
mecanografiado - matriz de grupo y suma de objetos - problema con la conversión de cadena a matriz

Aquí está el código -

Interfaz -

 interface IWEXInterface { readonly Date?: string; "Exec Qty"?: string; readonly Expiry?: string; }

Datos -

 let data: IWEXInterface[] = [ { Date: "8/30/2021", "Exec Qty": "13", Expiry: "17/09/2021", }, { Date: "8/30/2021", "Exec Qty": "15", Expiry: "17/09/2021", }, { Date: "8/30/2021", "Exec Qty": "13", Expiry: "17/09/2021", }, ];

Suma y agrupa los objetos -

 const sums = [ ...data .reduce((map, item) => { const { Date: key, "Exec Qty": qty } = item; const prev: IWEXInterface = map.get(key); if (prev) { prev["Exec Qty"]! += Number(qty); } else { map.set(key, Object.assign({}, item)); } return map; }, new Map()) .values(), ]; console.log(sums);

La salida -

 [ { Date: '8/30/2021', 'Exec Qty': '131513', Expiry: '17/09/2021' } ]

¿Cómo puedo convertir la cadena en número?

Salida deseada -

 [ { Date: '8/30/2021', 'Exec Qty': 41, Expiry: '17/09/2021' } ]

.................................................... .................................................... .........

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

0

intente reemplazar su código con este, devolverá una cadena pero la correcta.

 if (prev) { prev["Exec Qty"]! = (Number(prev["Exec Qty"])+ Number(qty)).toString() } else { map.set(key, Object.assign({}, item)); }
about 4 years ago · Juan Pablo Isaza Report

0

Simplemente emita prev["Exec Qty"] al número:

 prev["Exec Qty"] = String(Number(prev["Exec Qty"]!) + Number(qty));
about 4 years ago · Juan Pablo Isaza Report

0

¿Quizás no usar la abreviatura += ?

 prev["Exec Qty"] = Number(prev["Exec Qty"]) + Number(qty);
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!