Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

81
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!