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

134
Vistas
list comprehension containing enumerate equivalent in javascript

Please what is the equivalent in javascript of this python code

guessed_index = [
        i for i, letter in enumerate(self.chosen_word)
        if letter == self.guess
    ]

Both enumerate and list comprehension are not present in the ES6 equivalent, how to I combine both ideas into one

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

0

Perhaps findIndex is useful?

const word = "Hello";
const guess = "o";
const guessed_index = [...word].findIndex(letter => letter === guess);
console.log(guessed_index)
    

about 4 years ago · Juan Pablo Isaza Denunciar

0

Focussing on the iterable/enumerate part of your question rather than the specific task you're performing: You can implement a JavaScript analogue of Python's enumerate using a generator function, and consume the resulting generator (which is a superset of an iterable) via for-of (or manually if you prefer):

function* enumerate(it, start = 0) {
    let index = start;
    for (const value of it) {
        yield [value, index++];
    }
}

const word = "hello";
const guess = "l";
const guessed_indexes = [];
for (const [value, index] of enumerate(word)) {
    if (value === guess) {
        guessed_indexes.push(index);
    }
}
console.log(`guessed_indexes for '${guess}' in '${word}':`, guessed_indexes);

Or you can write a specific generator function that does the task of finding matches:

function* matchingIndexes(word, guess) {
    let index = 0;
    for (const letter of word) {
        if (letter === guess) {
            yield index;
        }
        ++index;
    }
}

const word = "hello";
const guess = "l";
const guessed_indexes = [...matchingIndexes(word, guess)];

console.log(`guessed_indexes for '${guess}' in '${word}':`, guessed_indexes);

about 4 years ago · Juan Pablo Isaza Denunciar

0

Just for clarity for potential readers that are not proficient in python, below is the python loop equivalent of your comprehension:

guessed_index = []
i = 0
for letter in self.chosen_word:
    if letter == self.guess:
        guessed_index.append(i)
    i += 1
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