简体   繁体   中英

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()
    })


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') 
        }

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()
        })
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM