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

249
Views
¿Cómo acumular todos los valores dentro de una matriz 2D que comparten el mismo campo?

Tengo una matriz 2D que contiene valores como este:

 var array = [["10/10/2020","1000"],["10/10/2020","300"],["07/10/2020","100"],["07/10/2020","100"],["03/10/2020","100"],["10/10/2020","100"]];

Para cada matriz anidada que tiene el mismo valor de fecha (el primer elemento), quiero sumar el segundo valor para tener algo como esto:

 arrayAdd = [["10/10/2020","1400"],["O7/10/2020","200"],["03/10/2020","100"]]

¿Cómo puedo hacer esto?

about 4 years ago · Santiago Gelvez
3 answers
Answer question

0

Paso a paso:

  1. Cree un objeto/mapa vacío.

  2. Iterar sobre su matriz.

    Obtenga el primer elemento de cada elemento en la matriz (fecha) y verifique si esa clave ya está en el objeto.

    • Si no es así, lo agregas. El valor será el segundo elemento (el número).

    • Si ya está allí, incrementa el valor.

  3. Object.entries convertirá ese objeto en una matriz con la forma que desee, como puede ver a continuación:

 const obj = { "10/10/2020": "1400", "O7/10/2020": "200", "03/10/2020": "100", }; console.log(Object.entries(obj));
 .as-console-wrapper { max-height: 100% !important; }

Esto debería ayudarte a empezar.

about 4 years ago · Santiago Gelvez Report

0

 var array = [["10/10/2020","1000"],["10/10/2020","300"],["07/10/2020","100"],["07/10/2020","100"],["03/10/2020","100"],["10/10/2020","100"]]; function fromEntries (iterable) { return [...iterable].reduce((obj, [key, val]) => { obj[key] = String(obj[key] ? +obj[key] + +val : val) return obj }, {}) } console.log(fromEntries(array))

about 4 years ago · Santiago Gelvez Report

0

 function mergeDates(array) { // create empty object mapping given data like {date: value, ...} const accumulator = {}; // iterate over all date value pairs for (const [date, value] of array) { // if accumulator already has date as key: add value if (date in accumulator) accumulator[date] += parseInt(value); // if not: add key with value else accumulator[date] = parseInt(value); } // turn {date: value, ...} into [[date, value], ...] return Object.entries(accumulator) // ...and convert value from integer into a string .map(([date, value]) => [date, value.toString()]); } console.log(mergeDates( [["10/10/2020","1000"],["10/10/2020","300"],["07/10/2020","100"],["07/10/2020","100"],["03/10/2020","100"],["10/10/2020","100"]] )); // expected results: [["10/10/2020","1400"],["07/10/2020","200"],["03/10/2020","100"]]

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!