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

147
Views
Necesito restar una clave, de una matriz de objetos, de otra clave de otra matriz de objetos

Tengo las siguientes dos matrices:

 const firstArray = [{startDate: 5, number: 15}, {startDate: 25, number: 10}, {startDate: 26, number: 10}]; const secondArray= [{startDay: 2, endDay:10, number: 15}, {startDay: 20, endDay:30, number: 20}];

si startDate está entre startDay y endDay , tengo que restar number de firstArray y el number de secondArray creando una nueva clave con resultado

Como resultado, tengo que poner una nueva clave en firstArray con el resultado:

 const firstArray = [{startDate: 5, number: 15, result: 0}, {startDate: 25, number: 25, result: -10}, {startDate: 26, number: 25, result: 0}];

si tengo más de una fecha de inicio en el mismo rango (entre el día de inicio y el día de finalización) tengo que agregar al último resultado de ese rango

El código que tengo por ahora:

 firstArray.map(el => ({...el, result: el.number - here's number from the secondArray according to the requirements}))
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

El mapa no funcionará muy bien para recorrer dos matrices y cambiar valores.

Simplemente use un ciclo for simple, verifique sus condiciones entre firstArray[i] y secondArray[i] , luego agregue el valor a firstArray[i].result

 const firstArray = [{startDate: 5, number: 15}, {startDate: 25, number: 20}]; const secondArray= [{startDay: 2, endDay:10, number: 10}, {startDay: 20, endDay:30, number: 20}]; for (let i = 0; i < Math.min(firstArray.length, secondArray.length); i++) if (secondArray[i].startDay < firstArray[i].startDate && firstArray[i].startDate < secondArray[i].endDay) firstArray[i].result = firstArray[i].number - secondArray[i].number; console.log(firstArray);

about 4 years ago · Juan Pablo Isaza Report

0

Cuando usa dos o más arreglos, necesita saber con qué índice está trabajando. Además, para obtener el mejor rendimiento, evite cualquier tipo de función de devolución de llamada, use bucles simples en su lugar:

 const firstArray = [{startDate: 5, number: 15}, {startDate: 25, number: 10}, {startDate: 26, number: 10}]; const secondArray= [{startDay: 2, endDay:10, number: 15}, {startDay: 20, endDay:30, number: 20}]; for(let i = 0, first, second, result = 0; i < firstArray.length; i++) { first = firstArray[i]; second = secondArray[i]; if (second && first.startDate >= second.startDay && first.startDate <= second.endDay) result = first.number - second.number; else result += first.number; first.result = result; } console.log(firstArray);

Usar map() también funcionaría y produciría un código un poco más corto, pero es más lento y muy innecesario:

 const firstArray = [{startDate: 5, number: 15}, {startDate: 25, number: 10}, {startDate: 26, number: 10}]; const secondArray= [{startDay: 2, endDay:10, number: 15}, {startDay: 20, endDay:30, number: 20}]; firstArray.map((first, i) => { const second = secondArray[i]; if (second && first.startDate >= second.startDay && first.startDate <= second.endDay) first.result = first.number - second.number; else first.result = ((firstArray[i-1]||{}).result || 0) + first.number; }); console.log(firstArray);

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!