代码之家  ›  专栏  ›  技术社区  ›  Sangram Badi

使用AngularJS或Javascript从现有json创建新json

  •  0
  • Sangram Badi  · 技术社区  · 8 年前

    类别JSON 我通过访问API并将其排序来获得这个JSON $范围。类别列表

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

    子类别JSON 我通过访问API并将其排序来获得这个JSON $范围。子类别列表

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

    我需要在下面的格式设计这个

    [
        {
            "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" : []
        }
    ]
    

    我有代码,但它没有显示完美的数据

    $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);
        });    
    });
    
    5 回复  |  直到 8 年前
        1
  •  2
  •   Zevgon    8 年前

    这应该会奏效。不像31piy的那么干净(哇!)但效率更高。(O(N+M)相对于O(N*M))

    const categoryList = [
      {
        "id": 1,
        "name": "Men"
      },
      {
        "id": 2,
        "name": "Women"
      },
      {
        "id": 3,
        "name": "Kids"
      }
    ];
    
    const subCategoryList = [
      {
        "id": 1,
        "category_id": 1,
        "name": "Footwear"
      },
      {
        "id": 2,
        "category_id": 2,
        "name": "Footwear"
      },
      {
        "id": 3,
        "category_id": 1,
        "name": "Cloths"
      }
    ];
    
    const mergeCategoryLists = (categoryList, subCategoryList) => {
      // Turn categoryList into an object with categoryId as key
      const categoryById = {};
      categoryList.forEach((category) => {
        categoryById[category.id] = {
          categoryName: category.name,
          categoryId: category.id,
          subCategory: []
        };
      });
     
      // Add subcategories
      subCategoryList.forEach((subCategory) => {
        const formattedSubCategory = {
          subCategoryId: subCategory.id,
          subCategoryName: subCategory.name
        };
        categoryById[subCategory.category_id].subCategory.push(formattedSubCategory);
      });
    
      // Convert categoryById into desired format
      return Object.values(categoryById);
    };
    
    console.log(mergeCategoryLists(categoryList, subCategoryList));
        2
  •  1
  •   Himanshu sharma    8 年前

    看看这个逻辑。

    $scope.newArray = angular.copy($scope.categoryList);
    $scope.catSubCat = []
    
    angular.forEach($scope.subcategoryList, function(subValue, subKey) {
        $scope.subCat = {
            'subCategoryId' : '',
            'subCategoryName' : ''
        }
        angular.forEach($scope.newArray, function(catValue, catKey) {
             $scope.subCat.subCategoryId = subValue.id;
                $scope.subCat.subCategoryName = subValue.name;
            if(subValue.category_id == catValue.id) {
               if(catValue.subCatName.hasOwnProperty('bye')){
                   $scope.newArray[catKey].subCatName = [];
                   $scope.newArray[catKey].subCatName.push($scope.subCat);
               }else{
                  $scope.newArray[catKey].subCatName.push($scope.subCat);
               }
            }
        });    
    });
    

    我们会进去吗 $scope.newArray

        3
  •  1
  •   31piy    8 年前

    您可以使用 Array#map 结合 Array#filter 为了达到预期结果:

    var categories = [{
        "id": 1,
        "name": "Men"
      },
      {
        "id": 2,
        "name": "Women"
      },
      {
        "id": 3,
        "name": "Kids"
      }
    ];
    
    var subcategories = [{
        "id": 1,
        "category_id": 1,
        "name": "Footwear"
      },
      {
        "id": 2,
        "category_id": 2,
        "name": "Footwear"
      },
      {
        "id": 3,
        "category_id": 1,
        "name": "Cloths"
      }
    ];
    
    var result = categories.map(cat => {
      return {
        categoryId: cat.id,
        categoryName: cat.name,
        subCategory: subcategories
          .filter(subc => subc.category_id === cat.id)
          .map(subc => {
            return {
              subCategoryId: subc.id,
              subCategoryName: subc.name
            };
          })
      };
    });
    
    console.log(result);
        4
  •  1
  •   misss-popcorn    8 年前
    var categoryList = [{
            "id": 1,
            "name": "Men"
        }, {
            "id": 2,
            "name": "Women"
        }, {
            "id": 3,
            "name": "Kids"
        }];
        var subCategoryList = [{
            "id": 1,
            "category_id": 1,
            "name": "Footwear"
        }, {
            "id": 2,
            "category_id": 2,
            "name": "Footwear"
        }, {
            "id": 3,
            "category_id": 1,
            "name": "Cloths"
        }];
    
        var finalJson = [];
        for (var i = 0; i < categoryList.length; i++) {
            var obj = {
                categoryId: categoryList[i].id,
                categoryName: categoryList[i].name,
                subCategory: []
            };
            var subCat = subCategoryList.filter(function(word) {
                return word.category_id === categoryList[i].id;
            });
            for (var j = 0; j < subCat.length; j++) {
                var obj2 = {
                    subCategoryId: subCat[j].id,
                    subCategoryName: subCat[j].name
                };
                obj.subCategory.push(obj2);
            }
            finalJson.push(obj);
        }
        console.log(finalJson);
    

    问题的纯Javascript解决方案,可以替换为 角度语法然后。。

        5
  •  1
  •   Syedur    8 年前

    使用以下代码:

    $scope.catSubCat = []
    angular.forEach($scope.categoryList, function(catValue, catKey) {
       var catObj = {
         'categoryId' : '',
         'categoryName' : '',
         'subCatName' : []
       }
       catObj.categoryId = catValue.id;
       catObj.categoryId = catValue.name;
        angular.forEach($scope.subcategoryList, function(subValue, subKey) {
            if(subValue.category_id == catValue.id) {           
                var subCatObj = {
                    'subCategoryId' : '',
                    'subCategoryName' : ''
                }
    
                subCatObj.subCategoryId = subValue.category_id;
                subCatObj.subCategoryName =  catValue.name;
    
                catObj.subCatName.push(subCatObj);
            }
        });  
        $scope.catSubCat.push(catObj);
    });