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

147
Visualizações
Incorrect value returns

I use this function:

function search (){
  v = [[2.0], [3.0], [4.0], [5.0]]
  if(v.indexOf(2.0) != -1){
    Logger.log ('if')
  }else{
    Logger.log('else')
  }
}

editing

My real need is in the values of words like this I was answered about numbers so I did not change and only added

function search (){
  v = [["One"], ["two"], ["three"], ["four"]]
  if(v.indexOf("One") != -1){
    Logger.log ('if')
  }else{
    Logger.log('else')
  }
}

And instead of "if" returning to me, "else" returns to me. I apologize for the broken English

about 4 years ago · Santiago Gelvez
3 Respostas
Responde à pergunta

0

You input array is a multi/two dimensional array. You are searching for 2.0 which is actually embedded inside another smaller array. Try changing the function to below.

function search() {
  v = [
    ["One"],
    ["two"],
    ["three"],
    ["four"]
  ]
  if (v.findIndex(e => e[0] === 'One') != -1) {
    console.log('if')
  } else {
    console.log('else')
  }
}

search();

about 4 years ago · Santiago Gelvez Relatório

0

Did you mean to write v = [2.0, 3.0, 4.0, 5.0];?

Because that will print out 'if'. Reason is that in your example, each element of v array is an array of it's own.

If you really want it to be an array of arrays, then to make it work you can do this in the if statement:

if ( v.flat().includes(2.0) )

  1. flat converts it from an array of array that contain a number, to an array of numbers.
  2. includes is a nicer method to check if the array contains a value - no need to check for index :)
about 4 years ago · Santiago Gelvez Relatório

0

array.indexOf() is not applicable for nested array. If your array is primitive types then it's working, but if array is non-primitive types then through indexOf you can't match the value.

Use this instated of your function:

v.findIndex(ele => [...new Set(ele)][0] == 2.0) >=0  ? 'if' : 'else'

enter image description here

about 4 years ago · Santiago Gelvez 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