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

85
Visualizações
Comparing data from JSON and input.value

I am working on small shopping cart project. I have products in JSON.file, also I have input for finding price of products. I am using class method question is: this are strings or numbers? -> (3) ['35', '35', '35']


searchItem(data) {
    let that = this

    searchBtn.addEventListener('click', function(e) {
      
       const input = document.querySelector('.input').value

           
       const findItem = data.filter(function(item) {
          if(item.price === input) {
            return item
          }
        })   // this return all data of product so I filtered only prices bellow


        const getItem = findItem.map(item => {
          return item.price
        })                      
 
       // this give:    (3) ['35', '35', '35']  


    
         if(input === getItem) {     
             console.log('same') 
        } else {
          console.log('try it again') 
        }
                         
         // this cond. show me :  console.log('try it again')
         // HOW TO GET:   console.log('same')  
      
       e.preventDefault()
    })


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

0

You can always run a typeof to find out what data types you dealing with

for example

console.log(typeof getItem[0])

also here :

        if(input === getItem) {     
             console.log('same') 
        } else {
          console.log('try it again') 
        }

you are checking the input variable against a whole array,you have to specify which item of the array to check against like :

if(input === getItem[0]) {     
             console.log('same') 
        } else {
          console.log('try it again') 
        }
about 4 years ago · Juan Pablo Isaza Relatório

0

A quick and straight to your question without the code analysis, Any value gotten from your input is a string and not numbers.

Therefore, if the values of the price you are getting from data are integer, then you will have to consider parsing the values from the input by using the parseInt() method. Example;

const findItem = data.filter(function(item) {
      if(item.price === parseInt(input)) {
        return item
      }
    })

Another thing is that getItem is an array so, evaluating with a particular value from input is bound to fail. Therefore, use getItem[0] instead and two "==" instead of "===" just as suggested on the comment.

function searchItem(data) {
        let that = this

        searchBtn.addEventListener('click', function(e) {
        
        const input = document.querySelector('.input').value

            
        const findItem = data.filter(function(item) {
            if(item.price == input) {
                return item
            }
        })   // this return all data of product so I filtered only prices bellow


            const getItem = findItem.map(item => {
                return item.price
            })  
            
            console.log(getItem);
    
        // this give:    (3) ['35', '35', '35']  

            if(input == getItem[0]) {     
                console.log('same') 
            } else {
                console.log('try it again') 
            }
                            
            // this cond. show me :  console.log('try it again')
            // HOW TO GET:   console.log('same')  
        
        e.preventDefault()
        })
    }
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