I just started with nodejs so i am confused how matching the strings work. I read about usage of =, == and ===.I have a json file I'm reading from and then I am iterating over that JSON and checking if a given string is equal to it or not. I got the json just fine and all, but I'm stuck at matching things.This code works :if (my_json[i].toLowerCase().trim() == 'My Value'.toLowerCase().trim()) { console.log('Found It'); break; } But, this one doesn't :var my_string; my_string = 'My Value'; if (my_json[i].toLowerCase().trim() == my_string.toLowerCase().trim()) { console.log('Found It'); break; } I did this to see the type of the variables and all :typeof(my_json[i].toLowerCase().trim()); typeof('My Value'.toLowerCase().trim()); typeof(my_string.toLowerCase().trim()); they all show as string. Then why isn't the matching working. I even tried with ===, but nothing changes. What am I doing wrong? or what would be a better way?
Submitted April 02, 2018 at 05:25PM by frizb3e
No comments:
Post a Comment