为什么这个字符串比较失败?

时间:2013-06-14 21:18:21

标签: angularjs foreach

基本上我尝试将交易与类别相匹配。我将交易标题发送给一个函数,如果有包含具有相同事务标题的关键字的类别,则返回类别ID。但它不起作用!

有什么问题?

$scope.data.push({
                title: temp[2],
                category: matchForCategory(temp[2]) 
            });

    //Try to match a transaction with a category
    function matchForCategory(transactionTitle){
        var temp = 0;

        angular.forEach($scope.categories, function(c){
            angular.forEach(c.keywords, function(k){
               if(k == transactionTitle)
                        temp =c.id;
            });
        });

        console.log(temp); //Output is always: 0

        if(temp == 0)
            return 'NA';
        else
            return temp;
    }

类别如下:

[...,{
    "id": 13,
    "title": "Utemat",
    "keywords": [
      "MCDONALD S VASAG",
      "PIZZERIA AMORE",
      "BISTRO65"
    ],
    "$$hashKey": "00T"
  },
  {
    "id": 14,
    "title": "Natklubb",
    "keywords": [

    ],
    "$$hashKey": "00V"
  },...]

1 个答案:

答案 0 :(得分:0)

可能是案例不匹配吗?尝试:

(k.toUpperCase() === transactionTitle.toUpperCase())