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

83
Vistas
return multiple numbers after entry point element in javascript

I want to search in text array,and find one of this element of entry array in text , and check that if the next element or elements are numbers, return the number or those numbers

But my code works just for 1 number element after entry element , as I mentioned before, I want multiple numbers after entry element, until it reaches an element of string type.

this is my code :

const entry = [
  'ENTRY', 'ZONE', 'ENTRI', 'ENTRE', 'ENTR', 'ZON', 'ZONI'
];

const text = ['HI', 'GOOD', 564, 'CLX', 'ENTRI', 'YYY', 'ENTRY', 657, 780, 34, 'XXX'];

const set = new Set(entry);

let result = [];
for (let i = 0; i < text.length; i++) {
  let curr = text[i],
    next = text[i + 1];
  if (set.has(curr) && typeof next == 'number') {
    result.push(next);
  }
}

console.log(result)

so this is output : //output : [ 657 ]

what i want : //output : [657 , 780 , 34]

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

0

You can add a nested loop on the same loop variable and add array values for as long they are numeric.

const entry = [
  'ENTRY', 'ZONE', 'ENTRI', 'ENTRE', 'ENTR', 'ZON', 'ZONI'
];

const text = ['HI', 'GOOD', 564, 'CLX', 'ENTRI', 'YYY', 'ENTRY', 657, 780, 34, 'XXX'];

const set = new Set(entry);

let result = [];
for (let i = 0; i < text.length; i++) {
  if (set.has(text[i])) {
    while (typeof text[i + 1] == "number") {
      result.push(text[++i]);
    }
  }
}

console.log(result)

about 4 years ago · Juan Pablo Isaza Denunciar

0

your condition says that the valid element has to come right after the set member, which only applied to the first element not every element.

a different way to do it is to turn on a flag that a member was found and turn it off once you reach another string that's not a member.

 const entry = ['ENTRY' , 'ZONE' , 'ENTRI' , 'ENTRE' , 'ENTR' ,'ZON' , 'ZONI'];

const text = ['hi' , 'good' , 564 , 'clx' , 'entri' , 'yyyy' , 'ENTRY' ,657 , 780 , 34 , 'xxxx'];

const set = new Set(entry);

let result = [];
let isAfterMatch = false
for (let i = 0; i < text.length; i++) {
  const curr = text[i];
  if (isAfterMatch && typeof curr === 'number') {
    result.push(curr);
  } else {
    isAfterMatch = set.has(curr)
  }
}

console.log(result)

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