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()
})
typeof myVariableToTest. By the way, the array contains strings. You could easily convert them all to numbers with something likearrayToConvert.map(i => +i);. Also, you are comparing an array to a string. What do you expect?? If you don't want to bother converting them to numbers you could take advantage of type coercion with using two==instead of three===.input(a string) withgetItem(an array). Tryif (input === getItem[0])but that is guaranteed to match, sincegetItemis filtered based on theinputvalue.