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

94
Visualizações
Return undefined when using reduce

Consider the following array:

const books = [
  {
    id: 1,
    name: 'A song of ice and fire',
    genre: 'Fantasy',
    author: {
      name: 'George R. R. Martin',
      birthYear: 1948,
    },
    releaseYear: 1991,
  },
  {
    id: 2,
    name: 'The lord of the rings',
    genre: 'Fantasy',
    author: {
      name: 'J. R. R. Tolkien',
      birthYear: 1892,
    },
    releaseYear: 1954,
  },
  {
];

Suppose I want to print the book with the longest title. The following works:

const longestBook = () => {
  const longestName = books.reduce((accumulator, book) => {
      if (book.name.length > accumulator.name.length) {
        return book;
      }
      return accumulator;
    });
  return longestName
}

console.log(longestBook().name);

My question is, why can't I return book.name / accumulator.name directly instead of using .name only when calling the function? If I try to do so, the result is undefined.

const longestBook = () => {
  const longestName = books.reduce((accumulator, book) => {
      if (book.name.length > accumulator.name.length) {
        return book.name;
      }
      return accumulator.name;
    });
  return longestName
}

console.log(longestBook());
about 4 years ago · Santiago Gelvez
3 Respostas
Responde à pergunta

0

If the accumulator becomes the .name property, it will be a plain string and no longer have a .name property to compare in the next iteration.

You'd need to be consistent with your accumulator type from the very start by providing an initial value. An empty string would suffice...

return books.reduce(
  (acc, { name }) => name.length > acc ? name : acc,  
  "" // initial value
);
about 4 years ago · Santiago Gelvez Relatório

0

With reduce, you're passing along a single value - the accumulator - from the previous iteration to the current iteration. In decently-structured reduce callbacks, the accumulator should usually stay the same shape throughout the loop, so that logic can be performed on it predictably and consistenly.

If you try to return the .name only, there are problems:

  const longestName = books.reduce((accumulator, book) => {
      if (book.name.length > accumulator.name.length) {
        return book.name;
      }
      return accumulator.name;
    });

because

  • Because you did not provide an initial value, the accumulator for the first iteration will be the first book object
  • Inside the first iteration, you return a .name from either the first book object or the second book object. The name is a string, so this results in the accumulator being a string for the second iteration
  • In the second iteration, the accumulator is now a string - not a book object - so return accumulator.name does not return anything.
about 4 years ago · Santiago Gelvez Relatório

0

You'll be accumulating a string instead of an object, so you're entire reduce() method needs to be geared towards that.

That also means you'll need to provide an initial value for reduce(). By default it takes the first value of the array, which is an object, and not a string.

const books = [{
  id: 1,
  name: 'A song of ice and fire',
  genre: 'Fantasy',
  author: {
    name: 'George R. R. Martin',
    birthYear: 1948,
  },
  releaseYear: 1991,
}, {
  id: 2,
  name: 'The lord of the rings',
  genre: 'Fantasy',
  author: {
    name: 'J. R. R. Tolkien',
    birthYear: 1892,
  },
  releaseYear: 1954,
}];


const longestBook = () => {
  const longestName = books.reduce((accumulator, { name }) => {
    if (name.length > accumulator.length) {
      return name;
    }
    return accumulator;
  }, '');
  return longestName;
}

console.log(longestBook());

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