When executing the command == between 2 equal string’s I am getting the return false.
The following code:
somewhere in the code:
Arr.prod.push({
"id" : product.id,
"nameProd" : product.name
});
In other local at code:
var id;
for(i in Arr.prod){
if( $.trim(str) == $.trim(Arr.prod[i].nameProd)){
id = Arr.prod[i].id;
break;
}
}
when i = 3, the value of Array.prod[i].nameProd is equal of str value. The value of these variables is: “DVD Player Automotivo CED229X – Tela 3, Entrada USB Frontal, Entrada SD Flip Down e Controle Remoto – Philips – Philips – Americ …”. But the comparison always returns false.
What is wrong?
EDIT
adding code in for iteration:
console.log( 'str = ' + $.trim(str).toLowerCase(), ', item = ' +
$.trim(Arr.prod[i].nameProd).toLowerCase(), 'result = ' +
($.trim(str).toLowerCase() == $.trim(Arr.prod[i].nameProd).toLowerCase()));
RESULT
str = dvd player automotivo ced229x – tela 3”, entrada usb frontal,
entrada sd flip down e controle remot … , item = tv 46″ led full hd
(1920 x 1080 pixels) – 46pfl7606d/78 – smart tv ambilight spectra 2,
online tv, c … result = falsestr = dvd player automotivo ced229x – tela 3”, entrada usb frontal,
entrada sd flip down e controle remot … , item = home theater c/ dvd
– 250 w rms, hdmi,divx, usb – hts3510/78 – philips – philips – americanas.com.br result = falsestr = dvd player automotivo ced229x – tela 3”, entrada usb frontal,
entrada sd flip down e controle remot … , item = notebook hp result
= falsestr = dvd player automotivo ced229x – tela 3”, entrada usb frontal,
entrada sd flip down e controle remot … , item = tenis rebook result
= falsestr = dvd player automotivo ced229x – tela 3”, entrada usb frontal,
entrada sd flip down e controle remot … , item = dvd player
automotivo ced229x – tela 3”, entrada usb frontal, entrada sd flip
down e controle remot … result = false
The last comparison should be true
look this
console.log('str : ' + escape(str));
console.log('arr : ' + escape(Arr.prod[i].nameProd)));
RESULT:
str : DVD%20Player%20Automotivo%20CED229X%20-%20Tela%203%27%27%2C%20Entrada%20USB%20Frontal%2C%20Entrada%20SD%20Flip%20Down%20e%20Controle%20Remot%20...
arr : DVD%20Player%20Automotivo%20CED229X%20-%20Tela%203%27%27%2C%20Entrada%20USB%20Frontal%2C%20Entrada%20SD%20Flip%20Down%A0e%20Controle%20Remot%20...
look after “Down” word in 2 log’s:
In str contains %20e%, as in arr contains %A0e%.
What is happening?
Thanks.
For some reason you code is generating a non-breaking space (ascii 160 or A0) after the word “Down” thus the
%A0.Why this happens i have no clue, but that is what you need to fix.
If you cannot figure out how to fix this problem, then you can at least treat the symptom by replacing the non-breaking spaces with normal spaces before comparison, like this: