如何比较angularjs中的两个对象数据

时间:2014-07-29 09:37:14

标签: arrays json angularjs foreach

我在控制器中有两个json对象,如何将第一个对象的id与angularjs中的第二个json对象id匹配  第一个json:

$scope.fetchAllCenters= [
        {
            "sid": 1,
            "group": "Group 1",
            "name": "Item 1"
        },
        {
            "sid": 2,
            "group": "Group 1",
            "name": "Item 2"
         },
        { "sid":3,
         "group":"Group2",
         "name":"Item 3"
        },


    ];

第二个json数据:

$scope.myData = [{name: "Moroni", id: 1},
                     {name: "Tiancum", id: 2},
                     {name: "Jacob", id: 3},
                     {name: "Nephi", id: 4},
                     {name: "Enos", id: 5}];

我如何将第一个sid与对象数据进行比较与angularjs中第二个对象数据的id匹配

1 个答案:

答案 0 :(得分:1)

if ($scope.fetchAllCenters[0].sid === $scope.myData[1].id) 
{
alert("sid of first fetchAllCenters is equat to id of myData ");
}