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

149
Vistas
how find the index of multiple elements in the same array
let secretMessage = ['Learning', 'is', 'not', 'about', 'what', 'you', 'get', 'easily', 'the', 'first', 'time,', 'it', 'is', 'about', 'what', 'you', 'can', 'figure', 'out.', '-2015,', 'Chris', 'Pine,', 'Learn', 'JavaScript'];
console.log(secretMessage.indexOf('get','easily')

Why does it only log the value of the 'get', not of both 'get' and 'easily', and how can I return both?

Appreciate the help, I am a beginner, sorry if the formatting is bad.

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

0

You can map indexOf over the list of values whose indices are wanted:

let secretMessage = ['Learning', 'is', 'not', 'about', 'what', 'you', 'get', 'easily', 'the', 'first', 'time,', 'it', 'is', 'about', 'what', 'you', 'can', 'figure', 'out.', '-2015,', 'Chris', 'Pine,', 'Learn', 'JavaScript'];

console.log(['get', 'easily'].map(x => secretMessage.indexOf(x)));

about 4 years ago · Juan Pablo Isaza Denunciar

0

The .indexOf() function syntax is as follows indexOf(searchElement, fromIndex) so what that means in your case is its searching for "get" starting from "easily" in the array.

To search both you can try this

console.log(secretMessage.indexOf('get'));
console.log(secretMessage.indexOf('easily'));

and if you need them together you can assign each to a variable then display them together later something like this

let one = secretMessage.indexOf('get');
let two = secretMessage.indexOf('easily');

let result = one + "," + two 

console.log(result);
about 4 years ago · Juan Pablo Isaza Denunciar

0

indexOf is a JavaScript array method that only can look for one element at a time, whatever value is being searched for in the array.

A more flexible array method for returning the indices of what you're looking for would be reduce, which lets you return whatever value you want after iterating, including another array with the indices of the values that match your search.

let secretMessage = ['Learning', 'is', 'not', 'about', 'what', 'you', 'get', 'easily', 'the', 'first', 'time,', 'it', 'is', 'about', 'what', 'you', 'can', 'figure', 'out.', '-2015,', 'Chris', 'Pine,', 'Learn', 'JavaScript'];

const matchingIndices = secretMessage.reduce((results, currentValue, currentIndex)=> {
  if (currentValue === 'get' || currentValue === 'easily'){
    results.push(currentIndex)
  }

  return results;
}, [])

console.log(matchingIndices) // => [6, 7]
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