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

244
Visualizações
find specific number in array for if statement

I know there are alot of questions like this on here but they keep pointing me to using includes and that is not working for me for some reason.

Goal - use an if statement to find out if a number exists in an array. (Don't care if its jQuery or plan JS)

I tried using includes but that failed:

testdata = [1,256]
testdata.includes(1) #or testdata.includes(256)
\\false

enter image description here

I also tried using indexof but that failed too:

testdata = [1,256]
testdata.indexOf('256') >= 0
\\false

enter image description here

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

0

Indeed, it looks like your array [1,256] is the first element of testdata array. In fact, you can see that testdata length is 1.

You should check if testdata[0].includes(1)

about 4 years ago · Juan Pablo Isaza Relatório

0

Your array is numbers but your .indexOf argument is a string.

The snippet below works fine.

If you need true/false, use a conditional block like:

let numberFound = false;

if (testdata.indexOf(256)>-1) {
  numberFound = true;
 } else {
  numberFound = false;
 }

Instead of the full if/else block, it is more usual to condense the conditional using the ternary operator

like this:

numberFound = (testdata.indexOf(256) > -1) ? true : false)

finally, if you're testing several entries, use a function to return true/false depending on what was passed to it:

function numberFound(number,array) {
  return (array.indexOf(number) > -1) ? true : false;
}

I've amended the snippet:

const testdata = [1,256]
console.log(testdata.indexOf(256));

console.log("test for 25: ", (testdata.indexOf(25) > -1) ? true : false);

console.log("test for 56: ", (testdata.indexOf(56) > -1) ? true : false);

console.log("test for 256: ", (testdata.indexOf(256) > -1) ? true : false);

function numberFound(number,array) {
  return (array.indexOf(number) > -1) ? true : false;
}

console.log("function testing 256:", numberFound(256,testdata));

console.log("function testing 25:", numberFound(25,testdata));

about 4 years ago · Juan Pablo Isaza Relatório

0

It is not working for you because you use it in the wrong way. # is the private identifier in JS. If you use // instead it works.

testdata = [1,256];

console.log(testdata.includes(1)); // or testdata.includes(256)

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