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

86
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 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