Category JSON I am getting this JSON by accessing API and soring it in $scope.categoryList

[
  {
    "id": 1,
    "name": "Men"
  },
  {
    "id": 2,
    "name": "Women"
  },
  {
    "id": 3,
    "name": "Kids"
  }
]

SubCategory JSON I am getting this JSON by accessing API and soring it in $scope.subCategoryList

[
  {
    "id": 1,
    "category_id": 1,
    "name": "Footwear"
  },
  {
    "id": 2,
    "category_id": 2,
    "name": "Footwear"
  },
  {
    "id": 3,
    "category_id": 1,
    "name": "Cloths"
  }
]

I need to design this in below format

[
    {
        "categoryId" : 1,
        "categoryName" : "Men",
        "subCategory" : [
            {
                "subCategoryId": 1,
                "subCategoryName": "Footwear"
            },
            {
                "subCategoryId": 3,
                "subCategoryName": "Cloths"
            },
        ]
    },
    {
        "categoryId" : 2,
        "categoryName" : "Women",
        "subCategory" : [
            {
                "subCategoryId": 2,
                "subCategoryName": "Footwear"
            }
        ]
    },
    {
        "categoryId" : 3,
        "categoryName" : "Kids",
        "subCategory" : []
    }
]

I have the code but it is not showing perfect data

$scope.catSubCat = []

angular.forEach($scope.subcategoryList, function(subValue, subKey) {
    $scope.subCat = {
        'subCategoryId' : '',
        'subCategoryName' : ''
    }
    angular.forEach($scope.categoryList, function(catValue, catKey) {
        if(subValue.category_id == catValue.id) {

            $scope.subCat.subCategoryId = subValue.id;
            $scope.subCat.subCategoryName = subValue.name;

            $scope.subCategory = {
                'categoryId' : '',
                'categoryName' : '',
                'subCatName' : []
            }

            $scope.catVal.categoryId = subValue.category_id;
            $scope.catVal.categoryName =  catValue.name;

            $scope.catVal.subCatName.push($scope.subCat);
        }
        $scope.catSubCat.push($scope.catVal);
    });    
});

Can anyone please help me on this.

share|improve this question
1  
"but it is not showing perfect data" What does it do wrong? What problem are you having fixing it? – T.J. Crowder 14 mins ago
    
JSON is a textual notation for data exchange. (More here.) If you're dealing with JavaScript source code, and not dealing with a string, you're not dealing with JSON. – T.J. Crowder 14 mins ago
    
what is the value of catSubCat you are getting? – Niladri 12 mins ago
    
catSubCat is root array – Sangram Badi 8 mins ago
    
If you can modify the api result by access the server code, I believe you should do it from the server side. – Aboodz 7 mins ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.