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

237
Views
reducir un Objeto basado en FECHA y HORA + calcular un VALOR

Tengo un objeto y quiero reducir la fecha y la hora juntas y calcular un número.
En este momento estoy probando el siguiente código (pero falta el valor de 'tiempo')...

 const myObject = [ { "date": "2023-01-01", "time": "18:00", "somevalue": 2 }, { "date": "2023-01-01", "time": "18:00", "somevalue": 5 }, { "date": "2023-01-02", "time": "19:00", "somevalue": 10 }, { "date": "2023-01-03", "time": "06:00", "somevalue": 13 } ]; const all = myObject.reduce( (prev, value) => ( { ...prev, [value.date]: (prev[value.date] || 0) + value.somevalue } ), {}); // to array console.log( Object.entries(all).map( ([key, value]) => ( {date: key, somevalue: value} ) ) );

Mi pregunta es: ¿Cómo puedo agregar el valor de "tiempo". Como esto:

 [ { "date": "2023-01-01", "time": "18:00", "somevalue": 7 }, { "date": "2023-01-02", "time": "19:00", "somevalue": 10 }, { "date": "2023-01-03", "time": "06:00", "somevalue": 13 } ]
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

¿Quizás algo como esto?

 const myObject = [ { date: "2023-01-01", time: "18:00", somevalue: 2 }, { date: "2023-01-01", time: "18:00", somevalue: 5 }, { date: "2023-01-02", time: "19:00", somevalue: 10 }, { date: "2023-01-03", time: "06:00", somevalue: 13 }]; const output = myObject.reduce((arr, acc) => { const index = arr.findIndex(({ date, time }) => date === acc.date && time === acc.time); //find duplicated elements by date if (index > -1) { // if you have duplicated element arr[index] = { ...arr[index], // merge objects somevalue: arr[index].somevalue + acc.somevalue // add value }; } else { arr = [...arr, acc]; //if no duplicates values, just add to array. } return arr; }, []); console.log(output)

Uso reduce para fusionar elementos duplicados por "fecha" y agrego someValue .

about 4 years ago · Juan Pablo Isaza Report

0

Esto también funcionaría

 const myObject = [ { date: "2023-01-01", time: "18:00", somevalue: 2, }, { date: "2023-01-01", time: "18:00", somevalue: 5, }, { date: "2023-01-02", time: "19:00", somevalue: 10, }, { date: "2023-01-03", time: "06:00", somevalue: 13, }, ]; const output = Object.entries( myObject.reduce((prev, { date, time, somevalue }) => { // create a key like <date>$<time> and add up // somevalue when key is discovered again prev[`${date}$${time}`] = prev[`${date}$${time}`] != undefined ? prev[`${date}$${time}`] + somevalue : somevalue; return prev; }, {}) ).map(([key, somevalue]) => { // split the key using $ and get date and time // return created object const [date, time] = key.split("$"); return { date, time, somevalue }; }); console.log(output);

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!