Iterating an array inside an array

时间:2015-09-14 16:11:17

标签: javascript arrays angularjs

I have a data-structure where i have an array inside an array which i need to iterate and compare. I need to compare my datasets array with patientList array whether they both have same id's

My DS:

$scope.data = [
    {
        "_id" : ObjectId("55f6e6d39a31fb5419c7b587"),
        "datasets" : [
                ObjectId("55f6c34e9e3cdc00273b57a3"),
                ObjectId("55f6c3569e3cdc00273b57a4"),
                ObjectId("55f6c3639e3cdc00273b57a5"),
                ObjectId("55f6c36e9e3cdc00273b57a6"),
                ObjectId("55f6c3789e3cdc00273b57a7")
        ],
        "__v" : 0
    },
    {
        "_id" : ObjectId("55f6e6d39a31fb5419c7b587"),
        "datasets" : [
                ObjectId("55f6c34e9e3cdc00273b57a3"),
                ObjectId("55f6c3569e3cdc00273b57a4"),
        ],
        "__v" : 0
    }
];

Code:

if ($scope.data.length > 0) {
    for (var i = 0; i < $scope.data.length; i++) {
        for (var j = i; j < $scope.data[i].datasets; j++) {
            if ($scope.data[i].datasets[j] == $scope.patientList[i]._id) {
                $scope.data[i].datasets[j].push($scope.patientList[i]);

            }
        }
    }
}

$scope.patientList is an array of objects having _id and patientname. The above code is not working.

2 个答案:

答案 0 :(得分:0)

j < $scope.data[i].datasets

Wasn't the limit supposed to be a numeric value, instead of a container?

I guess you want it to be j < $scope.date[i].datasets.length as well...

答案 1 :(得分:0)

我想,如果你放$scope.data[i].datasets.length.

,它会奏效