Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

114
Vistas
How to consecutively add values from one array to another and store the value in a new array in javascript?

I have 2 arrays:

const initialAmount = [50]

const transactionAmounts = [ -10, 10, 10, -1, -5, -10, 5, 5, 5, 10, 10, 10, 1, -1, -2, -5, -10 ]

How do i return an array that adds each value from transactionAmounts to the initialAmount? (excluding the first, which should be the initial value)

For example, it should return:

const valueAfterEachTransaction = [50, 40, 50, 60, 59, 54, 44, 49, 54, 59, 69, 79, 89, 90, 89, 87, 82, 72]

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You could take a closure over the sum and get all elememts of both arrays for mapping the acutal sum.

const
    initial = [50],
    transactions = [-10, 10, 10, -1, -5, -10, 5, 5, 5, 10, 10, 10, 1, -1, -2, -5, -10],
    result = [...initial, ...transactions].map((s => v => s += v)(0));

console.log(...result);

about 4 years ago · Juan Pablo Isaza Denunciar

0

const initialAmount = [50]

const transactionAmounts = [ -10, 10, 10, -1, -5, -10, 5, 5, 5, 10, 10, 10, 1, -1, -2, -5, -10 ]

const newArray = [initialAmount[0]]
let value = initialAmount[0]
let i = 0 

do {
    value = value + transactionAmounts[i]
  newArray[i+1] = value
  i++;
}
while (i < transactionAmounts.length);
 console.log(newArray)

const initialAmount = [50]

const transactionAmounts = [ -10, 10, 10, -1, -5, -10, 5, 5, 5, 10, 10, 10, 1, -1, -2, -5, -10 ]

const newArray = [initialAmount[0]]
let value = initialAmount[0]
let i = 0 

do {
    value = value + transactionAmounts[i]
  newArray[i+1] = value
  i++;
}
while (i < transactionAmounts.length);
 console.log(newArray)
about 4 years ago · Juan Pablo Isaza Denunciar

0

Maybe I'm missing something here, but this seems to be a straightforward map, after you handle the missing initial value.

const combine = ([x], ys) => [0, ...ys] .map (y => x + y)

const initialAmount = [50]
const transactionAmounts = [-10, 10, 10, -1, -5, -10, 5, 5, 5, 10, 10, 10, 1, -1, -2, -5, -10] 

console .log (combine (initialAmount, transactionAmounts))
.as-console-wrapper {max-height: 100% !important; top: 0}

I'm curious of what your underlying need is. This seems an odd requirement. Why is the initial value passed in an array? Why is there an implied 0 to add to the first one. Is this an XY problem?

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda