2016-07-17 31 views
0

我有两个JSON对象,一个包含主要学校名称列表,另一个包含所有学校名称列表。例如,说“编程学校”有四个地点。所以你有一个JSON对象编程学校,然后编程学校(阿拉斯加),编程学校(大众),编程学校(佛罗里达),编程学校(加州)。我想比较“编程学校”到所有四个位置,所以我经历了一个循环,将所有内容都分解到“(”并将所有内容都放在左边,所以我应该找到四次编程学校==编程学校。错误评估两个JSON字符串值

的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); 
    }) 
} 

我卡上为什么 “name.split(”( “)[0] ==温度” 的值不如果它不得不分裂一个“(”。如果我做了“语法学校==语法学校”,并且不必在“(”它评估为tru即但是如果我必须从上面列出的位置之一的括号中评估“编程学校==编程学校”,它不会评估为真。当我打印出“console.log(name.split(”(“)[0] +”“+ temp);”它会给我这个“编程学校”编程学校“,如果它不得不分裂在。一“(”第一编程学校报价并没有结束,我认为这是我的问题,但我不知道

+2

这是很难确定因为你没有分享实际的数据,但我的猜测是这个问题是''编程学校(阿拉斯加)“。split(”(“)[0] ==”Programming School“'< - 注意空间 – smarx

+1

一个简单的修复可能是使用'.split(“(”)[0 ]'< - 注意paren之前的空格 – smarx

+2

这是您的实际代码吗?如果是这样,为什么你只会在进行比较之后进行拆分? – azium

回答

0

理论修复基于评论讨论:

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); 
}) 
+0

是的,我只需要删除JSON.stringify()。谢谢大家 – ndb1995