I have a following json(sample data) which i get from the another URL
var data = [
{ id: 1, name: 'ravi', parentid: 0 },
{ id: 2, name: 'raj', parentid: 1 },
{ id: 3, name: 'ram', parentid: 1 },
{ id: 4, name: 'raja', parentid: 0 },
{ id: 5, name: 'raju', parentid: 4 },
{ id: 6, name: 'dinesh', parentid: 4 }
];
When i get success message in angular, i want to compare data which has id
of parentid zero with parentid of remaining data.I tried following code but i am not able to proceed more than this
$http.get('URL')
.success(function(data) {
var categories = data;
for(var i =0;i<=categories.length;i++)
{
if(typeof categories[i] != "undefined" && categories[i].parentId == 0)
{
$scope.cat.push(categories[i]);
}
if($scope.cat[i].parentid == categories[i].id)
{
$scope.content.push(categories[i]);
}
}
});
here i want to compare categories[i].parentId with $scope.cat.id and inserted into array but while comparing this i am getting an error like $scope.cat[i] is undefined
finally my output look like below
Ravi
raj
ram
raja
raju
dinesh
i<=categories.lengthshould bei<categories.lengthin your for loop$scope.catis defined. What is it supposed to be?