ndb1995 ndb1995 - 4 months ago 7
Javascript Question

Evaluating two JSON string values incorrectly

I have two JSON objects, one that holds a list of main school names, and another that holds a list of all school names. For instance, say "Programming School" has four locations. So you have Programming school in one JSON Object, and then Programming School (Alaska),Programming School (Mass),Programming School (Florida),Programming School (California). I want to compare "Programming School" to all four locations, so I go through a loop, split everything at the "(" and take everything to the left. So I should find that I have Programming School == Programming School four times.

index.html

<li ng-repeat="x in collegeList" >
<a class="list-item" href="who.html" id="{{x.collegeID}}" ng-click="getButtonClicked(x.collegeID,x.collegeName)">
<h3 class="name">
<span><img src="{{x.imagePath}}" alt="School Icon"> </span>{{x.collegeName}}
</h3>
</a>
</li>


displayData.js

$scope.getButtonClicked = function(idNumber,collegeName){
sessionStorage.setItem("id", idNumber);
sessionStorage.setItem("collegeName",collegeName);


angular.forEach($scope.data, function(x)
{
var name = JSON.stringify(x.collegeName); // gets the data value in a row in the column collegeName
var temp = JSON.stringify(collegeName); // gets the data value from the college selected on the first screen

alert("From Database: " + name.split('(')[0] );
alert("From College Picked: " + temp);
console.log(name + " " + temp);
console.log(name.split("(")[0] + " " + temp);
alert(name.split("(")[0] == temp);
})
}


I'm stuck on why "name.split("(")[0] == temp" does not evaluate to true if it had to split a "(". If I do "Grammar School == Grammar School", and didn't have to split at a "(" it evaluates to true. But if I have to evaluate "Programming School == Programming School" from one of the locations listed above that had a location in parenthesis, it does not evaluate to true.

When I print out "console.log(name.split("(")[0] + " " + temp);" it will give me this "Programming School "Programming School" if it had to split at a "(". The first Programming School does not end in quotations, I think that is my problem, but I am not sure.

Answer

Theoretical fix based on comment discussion:

angular.forEach($scope.data, function(x)
{
    var name = x.collegeName; // gets the data value in a row in the column collegeName
    var temp = collegeName; // gets the data value from the college selected on the first screen

    alert(name.split(" (")[0] === temp);
})