比较两个字符串在 javascript 中无法正常工作
Compare two strings is not working properly in javascript
这是我的 Javascript 函数,我在其中比较两个字符串,一个值作为函数参数接收,另一个值从 json 获取,后者来自 Spring 响应。
但我很惊讶我无法用 ===
和 ==
比较两个字符串,我可以在调试器中看到这些值。
我正在从我的 HTML 代码中调用我的函数:
<textarea onblur="searchProduct(this.value)"></textarea >
Javascript:
function searchProduct(value)
{
alert(value);
for (i in allProducts) {
console.log(allProducts[0].productName);
var tempProductName=allProducts[i].productName;
alert(tempProductName.toString() == value.toString());
// checks product name is present or not
if( tempProductName.toString() === value.toString()){
// setting other values details of products
$('#description').val(allProducts[i].productDescription);
$('.cost').val(allProducts[i].unitPrice);
}//if
}
}//searchProduct
allProducts: 来自 spring
的回复
[{"productId":3,"sku":"QTY","vendorProductId":"v1","productName":"P1name","productDescription":"p1 desc","supplierId":"s1","categoryId":"c","quantityPerUnit":1.0,"unitPrice":760.0,"msrp":1.0,"availableSize":40.0,"availableColors":"black,blue","size":40.0,"color":"white","discount":0.0,"unitWeight":1.0,"unitsInStock":148.0,"unitsOnOrder":500.0,"reorderLevel":150.0,"productAvailable":"Y","discountAvailable":"N","currentOrder":0.0,"pictureURL":"Picture path","ranking":10,"note":"Sample Notes","smsNotification":"N","productExpiryDate":null,"productDeleted":null,"alertExpiryDate":null,"idsku":"IDSKU1"},{"productId":4,"sku":"QTY","vendorProductId":"v1","productName":"P2name","productDescription":"p1 desc","supplierId":"s1","categoryId":"c","quantityPerUnit":1.0,"unitPrice":1.0,"msrp":1.0,"availableSize":40.0,"availableColors":"black,blue","size":40.0,"color":"white","discount":0.0,"unitWeight":1.0,"unitsInStock":150.0,"unitsOnOrder":500.0,"reorderLevel":150.0,"productAvailable":"Y","discountAvailable":"N","currentOrder":0.0,"pictureURL":"Picture path","ranking":10,"note":"Sample Notes","smsNotification":"N","productExpiryDate":null,"productDeleted":null,"alertExpiryDate":null,"idsku":"IDSKU1"},{"productId":5,"sku":"QTY","vendorProductId":"v1","productName":"P3name","productDescription":"p1 desc","supplierId":"s1","categoryId":"c","quantityPerUnit":1.0,"unitPrice":1.0,"msrp":1.0,"availableSize":40.0,"availableColors":"black,blue","size":40.0,"color":"white","discount":0.0,"unitWeight":1.0,"unitsInStock":1000.0,"unitsOnOrder":500.0,"reorderLevel":150.0,"productAvailable":"Y","discountAvailable":"N","currentOrder":0.0,"pictureURL":"Picture path","ranking":10,"note":"Sample Notes","smsNotification":"N","productExpiryDate":null,"productDeleted":null,"alertExpiryDate":null,"idsku":"IDSKU1"},{"productId":6,"sku":"QTY","vendorProductId":"v1","productName":"P4name","productDescription":"p1 desc","supplierId":"s1","categoryId":"c","quantityPerUnit":1.0,"unitPrice":1.0,"msrp":1.0,"availableSize":40.0,"availableColors":"black,blue","size":40.0,"color":"white","discount":0.0,"unitWeight":1.0,"unitsInStock":1000.0,"unitsOnOrder":500.0,"reorderLevel":150.0,"productAvailable":"Y","discountAvailable":"N","currentOrder":0.0,"pictureURL":"Picture path","ranking":10,"note":"Sample Notes","smsNotification":"N","productExpiryDate":null,"productDeleted":null,"alertExpiryDate":null,"idsku":"IDSKU1"},{"productId":7,"sku":"QTY","vendorProductId":"v1","productName":"P5name","productDescription":"p1 desc","supplierId":"s1","categoryId":"c","quantityPerUnit":1.0,"unitPrice":1.0,"msrp":1.0,"availableSize":40.0,"availableColors":"black,blue","size":40.0,"color":"white","discount":0.0,"unitWeight":1.0,"unitsInStock":1000.0,"unitsOnOrder":500.0,"reorderLevel":150.0,"productAvailable":"Y","discountAvailable":"N","currentOrder":0.0,"pictureURL":"Picture path","ranking":10,"note":"Sample Notes","smsNotification":"N","productExpiryDate":null,"productDeleted":null,"alertExpiryDate":null,"idsku":"IDSKU1"},{"productId":8,"sku":"QTY","vendorProductId":"v1","productName":"P6name","productDescription":"p1 desc","supplierId":"s1","categoryId":"c","quantityPerUnit":1.0,"unitPrice":1.0,"msrp":1.0,"availableSize":40.0,"availableColors":"black,blue","size":40.0,"color":"white","discount":0.0,"unitWeight":1.0,"unitsInStock":1000.0,"unitsOnOrder":500.0,"reorderLevel":150.0,"productAvailable":"Y","discountAvailable":"N","currentOrder":0.0,"pictureURL":"Picture path","ranking":10,"note":"Sample Notes","smsNotification":"N","productExpiryDate":null,"productDeleted":null,"alertExpiryDate":null,"idsku":"IDSKU1"},{"productId":9,"sku":"QTY","vendorProductId":"v1","productName":"P7name","productDescription":"p1 desc","supplierId":"s1","categoryId":"c","quantityPerUnit":1.0,"unitPrice":1.0,"msrp":1.0,"availableSize":40.0,"availableColors":"black,blue","size":40.0,"color":"white","discount":0.0,"unitWeight":1.0,"unitsInStock":1000.0,"unitsOnOrder":500.0,"reorderLevel":150.0,"productAvailable":"Y","discountAvailable":"N","currentOrder":0.0,"pictureURL":"Picture path","ranking":10,"note":"Sample Notes","smsNotification":"N","productExpiryDate":null,"productDeleted":null,"alertExpiryDate":null,"idsku":"IDSKU1"},{"productId":10,"sku":"QTY","vendorProductId":"v1","productName":"P8name","productDescription":"p1 desc","supplierId":"s1","categoryId":"c","quantityPerUnit":1.0,"unitPrice":1.0,"msrp":1.0,"availableSize":40.0,"availableColors":"black,blue","size":40.0,"color":"white","discount":0.0,"unitWeight":1.0,"unitsInStock":1000.0,"unitsOnOrder":500.0,"reorderLevel":150.0,"productAvailable":"Y","discountAvailable":"N","currentOrder":0.0,"pictureURL":"Picture path","ranking":10,"note":"Sample Notes","smsNotification":"N","productExpiryDate":null,"productDeleted":null,"alertExpiryDate":null,"idsku":"IDSKU1"},{"productId":11,"sku":"QTY","vendorProductId":"v1","productName":"P9name","productDescription":"p1 desc","supplierId":"s1","categoryId":"c","quantityPerUnit":1.0,"unitPrice":1.0,"msrp":1.0,"availableSize":40.0,"availableColors":"black,blue","size":40.0,"color":"white","discount":0.0,"unitWeight":1.0,"unitsInStock":1000.0,"unitsOnOrder":500.0,"reorderLevel":150.0,"productAvailable":"Y","discountAvailable":"N","currentOrder":0.0,"pictureURL":"Picture path","ranking":10,"note":"Sample Notes","smsNotification":"N","productExpiryDate":null,"productDeleted":null,"alertExpiryDate":null,"idsku":"IDSKU1"},{"productId":12,"sku":"QTY","vendorProductId":"v1","productName":"P9name","productDescription":"p1 desc","supplierId":"s1","categoryId":"c","quantityPerUnit":1.0,"unitPrice":1.0,"msrp":1.0,"availableSize":40.0,"availableColors":"black,blue","size":40.0,"color":"white","discount":0.0,"unitWeight":1.0,"unitsInStock":1000.0,"unitsOnOrder":500.0,"reorderLevel":150.0,"productAvailable":"Y","discountAvailable":"N","currentOrder":0.0,"pictureURL":"Picture path","ranking":10,"note":"Sample Notes","smsNotification":"N","productExpiryDate":null,"productDeleted":null,"alertExpiryDate":null,"idsku":"IDSKU1"},{"productId":13,"sku":"QTY","vendorProductId":"v1","productName":"P9name","productDescription":"p1 desc","supplierId":"s1","categoryId":"c","quantityPerUnit":1.0,"unitPrice":1.0,"msrp":1.0,"availableSize":40.0,"availableColors":"black,blue","size":40.0,"color":"white","discount":0.0,"unitWeight":1.0,"unitsInStock":1000.0,"unitsOnOrder":500.0,"reorderLevel":150.0,"productAvailable":"Y","discountAvailable":"N","currentOrder":0.0,"pictureURL":"Picture path","ranking":10,"note":"Sample Notes","smsNotification":"N","productExpiryDate":null,"productDeleted":null,"alertExpiryDate":null,"idsku":"IDSKU1"},{"productId":14,"sku":"QTY","vendorProductId":"v1","productName":"P9name","productDescription":"p1 desc","supplierId":"s1","categoryId":"c","quantityPerUnit":1.0,"unitPrice":1.0,"msrp":1.0,"availableSize":40.0,"availableColors":"black,blue","size":40.0,"color":"white","discount":0.0,"unitWeight":1.0,"unitsInStock":1000.0,"unitsOnOrder":500.0,"reorderLevel":150.0,"productAvailable":"Y","discountAvailable":"N","currentOrder":0.0,"pictureURL":"Picture path","ranking":10,"note":"Sample Notes","smsNotification":"N","productExpiryDate":null,"productDeleted":null,"alertExpiryDate":null,"idsku":"IDSKU1"},{"productId":15,"sku":"QTY","vendorProductId":"v1","productName":"P9name","productDescription":"p1 desc","supplierId":"s1","categoryId":"c","quantityPerUnit":1.0,"unitPrice":1.0,"msrp":1.0,"availableSize":40.0,"availableColors":"black,blue","size":40.0,"color":"white","discount":0.0,"unitWeight":1.0,"unitsInStock":1000.0,"unitsOnOrder":500.0,"reorderLevel":150.0,"productAvailable":"Y","discountAvailable":"N","currentOrder":0.0,"pictureURL":"Picture path","ranking":10,"note":"Sample Notes","smsNotification":"N","productExpiryDate":null,"productDeleted":null,"alertExpiryDate":null,"idsku":"IDSKU1"},{"productId":16,"sku":"QTY","vendorProductId":"v1","productName":"P10name","productDescription":"p1 desc","supplierId":"s1","categoryId":"c","quantityPerUnit":1.0,"unitPrice":1.0,"msrp":1.0,"availableSize":40.0,"availableColors":"black,blue","size":40.0,"color":"white","discount":0.0,"unitWeight":1.0,"unitsInStock":1000.0,"unitsOnOrder":500.0,"reorderLevel":150.0,"productAvailable":"Y","discountAvailable":"N","currentOrder":0.0,"pictureURL":"Picture path","ranking":10,"note":"Sample Notes","smsNotification":"N","productExpiryDate":null,"productDeleted":null,"alertExpiryDate":null,"idsku":"IDSKU1"}]
您可以使用 indexOf
和 length
:
实现您自己的字符串比较方法
String.prototype.equals = function (str2) {
return this.indexOf(str2) == 0 && this.length == str2.length;
}
或更 "PHP" 风格的方法:
function string_compare (str1, str2) {
return str1.indexOf(str2) == 0 && str1.length == str2.length;
}
JS字符串比较是区分大小写的,你可能需要不区分大小写的,例如:
tempProductName.toUpperCase() === value.toUpperCase()
而不是:
tempProductName.toString() === value.toString()
无论如何你不需要toString()
,因为该字段已经是一个字符串。
查看您的代码,这应该可以。
尝试更改迭代器 - for..in
用于迭代对象(即字典)。要遍历数组,请使用常规 for
或 forEach
:
function searchProduct(value) {
allProducts
.filter(function(product) {
return product[productName] === value
})
.forEach(function(product) {
$('#description').val(product.productDescription);
$('.cost').val(product.unitPrice);
});
}
这是我的 Javascript 函数,我在其中比较两个字符串,一个值作为函数参数接收,另一个值从 json 获取,后者来自 Spring 响应。
但我很惊讶我无法用 ===
和 ==
比较两个字符串,我可以在调试器中看到这些值。
我正在从我的 HTML 代码中调用我的函数:
<textarea onblur="searchProduct(this.value)"></textarea >
Javascript:
function searchProduct(value)
{
alert(value);
for (i in allProducts) {
console.log(allProducts[0].productName);
var tempProductName=allProducts[i].productName;
alert(tempProductName.toString() == value.toString());
// checks product name is present or not
if( tempProductName.toString() === value.toString()){
// setting other values details of products
$('#description').val(allProducts[i].productDescription);
$('.cost').val(allProducts[i].unitPrice);
}//if
}
}//searchProduct
allProducts: 来自 spring
的回复[{"productId":3,"sku":"QTY","vendorProductId":"v1","productName":"P1name","productDescription":"p1 desc","supplierId":"s1","categoryId":"c","quantityPerUnit":1.0,"unitPrice":760.0,"msrp":1.0,"availableSize":40.0,"availableColors":"black,blue","size":40.0,"color":"white","discount":0.0,"unitWeight":1.0,"unitsInStock":148.0,"unitsOnOrder":500.0,"reorderLevel":150.0,"productAvailable":"Y","discountAvailable":"N","currentOrder":0.0,"pictureURL":"Picture path","ranking":10,"note":"Sample Notes","smsNotification":"N","productExpiryDate":null,"productDeleted":null,"alertExpiryDate":null,"idsku":"IDSKU1"},{"productId":4,"sku":"QTY","vendorProductId":"v1","productName":"P2name","productDescription":"p1 desc","supplierId":"s1","categoryId":"c","quantityPerUnit":1.0,"unitPrice":1.0,"msrp":1.0,"availableSize":40.0,"availableColors":"black,blue","size":40.0,"color":"white","discount":0.0,"unitWeight":1.0,"unitsInStock":150.0,"unitsOnOrder":500.0,"reorderLevel":150.0,"productAvailable":"Y","discountAvailable":"N","currentOrder":0.0,"pictureURL":"Picture path","ranking":10,"note":"Sample Notes","smsNotification":"N","productExpiryDate":null,"productDeleted":null,"alertExpiryDate":null,"idsku":"IDSKU1"},{"productId":5,"sku":"QTY","vendorProductId":"v1","productName":"P3name","productDescription":"p1 desc","supplierId":"s1","categoryId":"c","quantityPerUnit":1.0,"unitPrice":1.0,"msrp":1.0,"availableSize":40.0,"availableColors":"black,blue","size":40.0,"color":"white","discount":0.0,"unitWeight":1.0,"unitsInStock":1000.0,"unitsOnOrder":500.0,"reorderLevel":150.0,"productAvailable":"Y","discountAvailable":"N","currentOrder":0.0,"pictureURL":"Picture path","ranking":10,"note":"Sample Notes","smsNotification":"N","productExpiryDate":null,"productDeleted":null,"alertExpiryDate":null,"idsku":"IDSKU1"},{"productId":6,"sku":"QTY","vendorProductId":"v1","productName":"P4name","productDescription":"p1 desc","supplierId":"s1","categoryId":"c","quantityPerUnit":1.0,"unitPrice":1.0,"msrp":1.0,"availableSize":40.0,"availableColors":"black,blue","size":40.0,"color":"white","discount":0.0,"unitWeight":1.0,"unitsInStock":1000.0,"unitsOnOrder":500.0,"reorderLevel":150.0,"productAvailable":"Y","discountAvailable":"N","currentOrder":0.0,"pictureURL":"Picture path","ranking":10,"note":"Sample Notes","smsNotification":"N","productExpiryDate":null,"productDeleted":null,"alertExpiryDate":null,"idsku":"IDSKU1"},{"productId":7,"sku":"QTY","vendorProductId":"v1","productName":"P5name","productDescription":"p1 desc","supplierId":"s1","categoryId":"c","quantityPerUnit":1.0,"unitPrice":1.0,"msrp":1.0,"availableSize":40.0,"availableColors":"black,blue","size":40.0,"color":"white","discount":0.0,"unitWeight":1.0,"unitsInStock":1000.0,"unitsOnOrder":500.0,"reorderLevel":150.0,"productAvailable":"Y","discountAvailable":"N","currentOrder":0.0,"pictureURL":"Picture path","ranking":10,"note":"Sample Notes","smsNotification":"N","productExpiryDate":null,"productDeleted":null,"alertExpiryDate":null,"idsku":"IDSKU1"},{"productId":8,"sku":"QTY","vendorProductId":"v1","productName":"P6name","productDescription":"p1 desc","supplierId":"s1","categoryId":"c","quantityPerUnit":1.0,"unitPrice":1.0,"msrp":1.0,"availableSize":40.0,"availableColors":"black,blue","size":40.0,"color":"white","discount":0.0,"unitWeight":1.0,"unitsInStock":1000.0,"unitsOnOrder":500.0,"reorderLevel":150.0,"productAvailable":"Y","discountAvailable":"N","currentOrder":0.0,"pictureURL":"Picture path","ranking":10,"note":"Sample Notes","smsNotification":"N","productExpiryDate":null,"productDeleted":null,"alertExpiryDate":null,"idsku":"IDSKU1"},{"productId":9,"sku":"QTY","vendorProductId":"v1","productName":"P7name","productDescription":"p1 desc","supplierId":"s1","categoryId":"c","quantityPerUnit":1.0,"unitPrice":1.0,"msrp":1.0,"availableSize":40.0,"availableColors":"black,blue","size":40.0,"color":"white","discount":0.0,"unitWeight":1.0,"unitsInStock":1000.0,"unitsOnOrder":500.0,"reorderLevel":150.0,"productAvailable":"Y","discountAvailable":"N","currentOrder":0.0,"pictureURL":"Picture path","ranking":10,"note":"Sample Notes","smsNotification":"N","productExpiryDate":null,"productDeleted":null,"alertExpiryDate":null,"idsku":"IDSKU1"},{"productId":10,"sku":"QTY","vendorProductId":"v1","productName":"P8name","productDescription":"p1 desc","supplierId":"s1","categoryId":"c","quantityPerUnit":1.0,"unitPrice":1.0,"msrp":1.0,"availableSize":40.0,"availableColors":"black,blue","size":40.0,"color":"white","discount":0.0,"unitWeight":1.0,"unitsInStock":1000.0,"unitsOnOrder":500.0,"reorderLevel":150.0,"productAvailable":"Y","discountAvailable":"N","currentOrder":0.0,"pictureURL":"Picture path","ranking":10,"note":"Sample Notes","smsNotification":"N","productExpiryDate":null,"productDeleted":null,"alertExpiryDate":null,"idsku":"IDSKU1"},{"productId":11,"sku":"QTY","vendorProductId":"v1","productName":"P9name","productDescription":"p1 desc","supplierId":"s1","categoryId":"c","quantityPerUnit":1.0,"unitPrice":1.0,"msrp":1.0,"availableSize":40.0,"availableColors":"black,blue","size":40.0,"color":"white","discount":0.0,"unitWeight":1.0,"unitsInStock":1000.0,"unitsOnOrder":500.0,"reorderLevel":150.0,"productAvailable":"Y","discountAvailable":"N","currentOrder":0.0,"pictureURL":"Picture path","ranking":10,"note":"Sample Notes","smsNotification":"N","productExpiryDate":null,"productDeleted":null,"alertExpiryDate":null,"idsku":"IDSKU1"},{"productId":12,"sku":"QTY","vendorProductId":"v1","productName":"P9name","productDescription":"p1 desc","supplierId":"s1","categoryId":"c","quantityPerUnit":1.0,"unitPrice":1.0,"msrp":1.0,"availableSize":40.0,"availableColors":"black,blue","size":40.0,"color":"white","discount":0.0,"unitWeight":1.0,"unitsInStock":1000.0,"unitsOnOrder":500.0,"reorderLevel":150.0,"productAvailable":"Y","discountAvailable":"N","currentOrder":0.0,"pictureURL":"Picture path","ranking":10,"note":"Sample Notes","smsNotification":"N","productExpiryDate":null,"productDeleted":null,"alertExpiryDate":null,"idsku":"IDSKU1"},{"productId":13,"sku":"QTY","vendorProductId":"v1","productName":"P9name","productDescription":"p1 desc","supplierId":"s1","categoryId":"c","quantityPerUnit":1.0,"unitPrice":1.0,"msrp":1.0,"availableSize":40.0,"availableColors":"black,blue","size":40.0,"color":"white","discount":0.0,"unitWeight":1.0,"unitsInStock":1000.0,"unitsOnOrder":500.0,"reorderLevel":150.0,"productAvailable":"Y","discountAvailable":"N","currentOrder":0.0,"pictureURL":"Picture path","ranking":10,"note":"Sample Notes","smsNotification":"N","productExpiryDate":null,"productDeleted":null,"alertExpiryDate":null,"idsku":"IDSKU1"},{"productId":14,"sku":"QTY","vendorProductId":"v1","productName":"P9name","productDescription":"p1 desc","supplierId":"s1","categoryId":"c","quantityPerUnit":1.0,"unitPrice":1.0,"msrp":1.0,"availableSize":40.0,"availableColors":"black,blue","size":40.0,"color":"white","discount":0.0,"unitWeight":1.0,"unitsInStock":1000.0,"unitsOnOrder":500.0,"reorderLevel":150.0,"productAvailable":"Y","discountAvailable":"N","currentOrder":0.0,"pictureURL":"Picture path","ranking":10,"note":"Sample Notes","smsNotification":"N","productExpiryDate":null,"productDeleted":null,"alertExpiryDate":null,"idsku":"IDSKU1"},{"productId":15,"sku":"QTY","vendorProductId":"v1","productName":"P9name","productDescription":"p1 desc","supplierId":"s1","categoryId":"c","quantityPerUnit":1.0,"unitPrice":1.0,"msrp":1.0,"availableSize":40.0,"availableColors":"black,blue","size":40.0,"color":"white","discount":0.0,"unitWeight":1.0,"unitsInStock":1000.0,"unitsOnOrder":500.0,"reorderLevel":150.0,"productAvailable":"Y","discountAvailable":"N","currentOrder":0.0,"pictureURL":"Picture path","ranking":10,"note":"Sample Notes","smsNotification":"N","productExpiryDate":null,"productDeleted":null,"alertExpiryDate":null,"idsku":"IDSKU1"},{"productId":16,"sku":"QTY","vendorProductId":"v1","productName":"P10name","productDescription":"p1 desc","supplierId":"s1","categoryId":"c","quantityPerUnit":1.0,"unitPrice":1.0,"msrp":1.0,"availableSize":40.0,"availableColors":"black,blue","size":40.0,"color":"white","discount":0.0,"unitWeight":1.0,"unitsInStock":1000.0,"unitsOnOrder":500.0,"reorderLevel":150.0,"productAvailable":"Y","discountAvailable":"N","currentOrder":0.0,"pictureURL":"Picture path","ranking":10,"note":"Sample Notes","smsNotification":"N","productExpiryDate":null,"productDeleted":null,"alertExpiryDate":null,"idsku":"IDSKU1"}]
您可以使用 indexOf
和 length
:
String.prototype.equals = function (str2) {
return this.indexOf(str2) == 0 && this.length == str2.length;
}
或更 "PHP" 风格的方法:
function string_compare (str1, str2) {
return str1.indexOf(str2) == 0 && str1.length == str2.length;
}
JS字符串比较是区分大小写的,你可能需要不区分大小写的,例如:
tempProductName.toUpperCase() === value.toUpperCase()
而不是:
tempProductName.toString() === value.toString()
无论如何你不需要toString()
,因为该字段已经是一个字符串。
查看您的代码,这应该可以。
尝试更改迭代器 - for..in
用于迭代对象(即字典)。要遍历数组,请使用常规 for
或 forEach
:
function searchProduct(value) {
allProducts
.filter(function(product) {
return product[productName] === value
})
.forEach(function(product) {
$('#description').val(product.productDescription);
$('.cost').val(product.unitPrice);
});
}