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

182
Views
Cómo calcular el elemento a dividir en partes iguales

en mi empresa, los grandes datos se guardan como un archivo de Excel y he convertido los datos a un archivo JSON para su procesamiento, pero es difícil para un novato como yo

 var Task = [{ id1: 01, id2: 02, money: 400 },{ id1: 03, id2: 02, money: 200 },{ id1: 01, id2: 02, id3: 03, money: 300 }]

lo difícil que enfrento es cómo ponerlos en 1 matriz de empleados, con una cantidad correspondiente, por ejemplo, la tarea tiene 3 id (id1, id2, id3) entonces el dinero se dividirá entre 3, una tarea con 2 id se dividirá por 2

lo que quiero

 var employee = [{ id: 001, money: 400 },{ id: 002, money: 400 },{ id: 003, money: 100 }]

Probé el siguiente código, pero solo se puede aplicar con 1 identificación única como primera identificación

 var nv = nhanvien.reduce((acc, cur) => { var existingnhanvien = nhanvien.find(o => o.id1 === cur.id ) if (existingnhanvien) { existingnhanvien.money= parseInt(existingnhanvien.money+ cur.money) } else { acc.push({ id: cur.id1, money: cur.money }) } return acc }, [])
about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

 var Task = [{ id1: 01, id2: 02, money: 400 },{ id1: 03, id2: 02, money: 200 },{ id1: 01, id2: 02, id3: 03, money: 300 }]; let employee = []; for(t of Task) { let ids = []; if(t.hasOwnProperty("id1")) { ids.push(t.id1); } if(t.hasOwnProperty("id2")) { ids.push(t.id2); } if(t.hasOwnProperty("id3")) { ids.push(t.id3); } let money = t.money/ids.length; for(let i of ids) { let exists = false; for(let e of employee) { if(i == e.id) { exists = true; e.money += money; } } if(!exists) { employee.push({id: i, money: money}) } } } console.log(employee);

about 4 years ago · Santiago Trujillo Report

0

Repita su matriz de Task y, para cada objeto, extraiga las claves.

 var Task = [{ id1: 01, id2: 02, money: 400 }, { id1: 03, id2: 02, money: 200 }, { id1: 01, id2: 02, id3: 03, money: 300 }, { id1: 01, id2: 02, id3: 03, id4: 04, id5: 05, id6: 06, money: 300 }] let obj = {} for (let t of Task) { let money = t.money let keys = Object.keys(t) // ALL KEYS EXCEPT OF "money" keys.splice(Object.keys(t).indexOf('money')) // SPLIT MONEY BETWEEN IDs let chunk = money / keys.length for (k of keys) { if (typeof obj[t[k]] == 'undefined') { // IF EMPLOYEE DOESN'T EXIST IN OBJECT, INITIALIZE IT obj[t[k]] = chunk } else { // IF EMPLOYEE EXISTS IN OBJECT, INCREMENT ITS VALUE obj[t[k]] += chunk } } } let employees = Object.keys(obj).map(key => { return { id: key, money: obj[key] } }) console.log(employees)

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