如何使用角度基于匹配的条件打印匹配和不匹配?

时间:2016-05-10 04:59:29

标签: javascript angularjs

{
    "_id": {
        "$oid": "5705f793e4b0acd6e2456804a"
    },
    "Categories": [
        {
            "mainmodels": [
                {
                    "submodels": [
                        {
                            "price": "2000",
                            "submodelname": "lumia021",
                            "Remainingphones": "2",
                            "Bookedphones": "8",
                            "Numofphones": "10"
                        },
                        {
                            "price": "4000",
                            "submodelname": "lumia K6",
                            "Remainingphones": "0",
                            "Bookedphones": "15",
                            "Numofphones": "15"
                        }

                    ],
                    "Status": "Active",
                    "modelname": "lumia",
                    "fromdate": "2016-04-01T16:39:12.051Z",
                    "todate": "2016-04-31T19:19:44.051Z"
                }
            ],
            "brand": "nokia"
        }
    ],
    "rank": "1",
    "name": "kalasipalaya"
}

我已经给出了上面的对象我需要检查每个子模型(这里有两个sumodels)Numofphones和Bookedphones是匹配的。如果两者(这里我给了两个子模型)Numofphones和Bookedphones匹配我需要打印匹配否则我需要打印不匹配我怎么能解决这个帮助我。

2 个答案:

答案 0 :(得分:0)

   //This will take to submodel array of object

    var _getSubModel = m[0].Categories[0].mainmodels[0].submodels;
    var _newArray2 = []; 

    //Checking if Bookedphones of first object submodel, is same with other objects.

    var _newArray = _getSubModel.filter(function(item){
       return item.Bookedphones == _getSubModel[0].Bookedphones;
    })
    // If all the Bookedphones are same then length of _newArray & submodel will be same.
   // If same then check for Numofphones

    if(_getSubModel.length == _newArray.length){
     _newArray2 =  _getSubModel.filter(function(item){
       return item.Numofphones == _getSubModel[0].Numofphones;
    })
    // If all Numofphones are same,then length of _newArray2 & submodel will be same
    if(_getSubModel.length == _newArray2.length){
    console.log('Matched');
    }
     else{
     console.log('Not Matched');
     }
    }
    else{
    console.log('Not Matched');
    }

选中此jsfiddle

答案 1 :(得分:0)

您可以使用custom filter之类的内容:

        var result = [];
        angular.forEach(submodels, function (submodel) {
            if(submodel.Numofphones == submodel.Bookedphones)
                result.push(submodel);
        });
        return result;

http://jsfiddle.net/y7r1xe0t/236/

更新: http://jsfiddle.net/y7r1xe0t/237/

return function (submodels, matched_or_not) {
        var result = [];
        angular.forEach(submodels, function (submodel) {
            if(matched_or_not && submodel.Numofphones == submodel.Bookedphones)
                result.push(submodel);
            else if(!matched_or_not && submodel.Numofphones != submodel.Bookedphones)
                result.push(submodel);
        });
        return result;
};

过滤器会在您发送true时返回匹配的对象。

enter image description here