Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

397
Visualizações
This JS code finds the index of the highest value in an array but how does it work?

The line of code in quesion:

let largest = arr.reduce((a,v,i) => v > a[1] ? [i,v] : a, [-1,0]);

I am confused mainly by this part a, [-1,0], I don't understand this syntax. What is a doing before a comma?

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Here's how it looks with a lot of the shorthand expanded:

const reducer = (accumulator, currentValue, index) => {
  if (currentValue > accumulator[1]) {
    return([index, currentValue])
  } else {
    return(accumulator)
  }
}

let largest = accumulator.reduce(reducer, [-1, 0])

What's happening:

Array.prototype.reduce() has two arguments, the callback function and the initial value. That's where the [-1, 0] is coming from, that's the "initial value" that will be passed as the "accumulator" to the callback function on the first iteration.

The shortest shorthand for a function in javascript is () => value where "value" represents the return value of the function—no parenthesis are required. In this example that return value is a ternary statement: condition ? return if true : return if false. Essentially, because they're using a single line to produce a single value, there's no parenthesis or braces required around the function (which ends at the a).

The callback function you pass to Array.reduce() can take three arguments, (accumulator, currentValue, index).

The key here is in the initial value, which is [index, value], which keeps track of both until iteration is finished, in which case just the final value is returned.

over 4 years ago · Santiago Trujillo Relatório

0

The array that comes after the comma is a second argument to .reduce(). The code there is equivalent to

let largest = arr.reduce(
  (a,v,i) => {
    return v > a[1] ? [i,v] : a;
  },
  [-1,0] // second argument
);

On very iteration, the accumulator is an array composed of 2 values. The value returned from the callback becomes the new accumulator in the next iteration. The value returned on the last iteration becomes the result of the whole .reduce() expression - the largest.

So what return v > a[1] ? [i,v] : a; does inside a .reduce() callback is it'll go through the array and find the item with the highest v - once the [i, v] of that becomes the accumulator, the accumulator will stay as that array until iteration finishes.

over 4 years ago · Santiago Trujillo Relatório

0

This is more easily understood if formalized and expanded:

let reducer = (a,v,i) => {
  if (v > a[1])
    return [i,v];

  return a
};

let largest = arr.reduce(reducer, [-1,0]);

Where now the reducer function argument is pretty clear.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda