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

269
Vistas
Uncaught TypeError: element is undefined when using generator function

I have the following javascript snippet:


/**
 *  Returns the maximal element in the `iterable` as calculated by the given function
 *  @param {Iterable} iterable - iterable to find the maximal element in
 *  @param {function} fn - function to calculate the maximal element
 * 
 *  @returns {any} - maximal element in the `iterable`
 */
function maxBy(iterable, fn) {
    let maximalElement = iterable[0];
    for (let element of iterable) {
        if (fn(element) > fn(maximalElement)) {
            maximalElement = element;
        }
    }
    return maximalElement;

}


// example generator function
generator = function* ()  {
   yield [3,4]
   yield [4,6]
}
maxBy(generator(), element => element[1])

When I run this in browser console, I get Uncaught TypeError: element is undefined error and I can't seem to spot where's the error in my code.

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

0

You cannot use iterable[0] to access the first element in a generator. You'll need to get the iterator and iterate it:

function maxBy(iterable, fn) {
    const iterator = iterable[Symbol.iterator]();
    const first = iterator.next();
    if (first.done) return;
    let maximalElement = first.value;
    for (let element of iterator) {
        if (fn(element) > fn(maximalElement)) {
            maximalElement = element;
        }
    }
    return maximalElement;
}

Alternatively, just initialise maximalElement with undefined, this might happen anyway if the iterable is empty:

function maxBy(iterable, fn) {
    let maximalElement;
    for (let element of iterable) {
        if (maximalElement === undefined || fn(element) > fn(maximalElement)) {
            maximalElement = element;
        }
    }
    return maximalElement;

}
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