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

188
Visualizações
How to go about making an array of the items between two keywords of another array?

Sounds way more confusing than it actually is, but it is simple given an example:

Let's say I have an array like this - [1, 2, 3, 4, 5]

I want the numbers 2, 3 and 4 in their own array like this - [2, 3, 4]

One detail is that the array is completely random! It is all user inputted so I use keywords to find the section I want, like this - ["cmd", "|arg", "stuff", "blah", "|end"]

For this example I would like an array of the items between the keywords |arg and |end that looks like this - ["stuff", "blah"]

I have already found the position in the array of the two keywords but how would I go about making an array of the items between these keywords?

I have tried splicing and I have tried for, but for is not allowed in the game I am coding for and I just cannot seem to find out how to splice it. There has to be a better way and I am not sure what the Method would be called if there even is a Method that can accomplish this.

I don't have any real code to show, as it would be a complete and utter mess if I show it

Just started learning javascript 3 days ago

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

If you first check to see if that the array contains both values, then you can slice the array into another result array.

let ar = ["cmd", "|arg", "stuff", "blah", "|end"];
let arRes = [];

if(ar.includes("|arg") && ar.includes("|end")){
  let idx1 = ar.indexOf("|arg") + 1;
  let idx2 = ar.indexOf("|end");
  arRes = ar.slice(idx1, idx2)
}

console.log(arRes)

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