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

使用AngularJS或Javascript从现有json创建新json,javascript,angularjs,json,Javascript,Angularjs,Json,类别JSON 我通过访问API并将其排序到$scope.categoryList [ { "id": 1, "name": "Men" }, { "id": 2, "name": "Women" }, { "id": 3, "name": "Kids" } ] [ { "id": 1, "category_id": 1, "name": "Footwear" }, { "id":

类别JSON 我通过访问API并将其排序到$scope.categoryList

[
  {
    "id": 1,
    "name": "Men"
  },
  {
    "id": 2,
    "name": "Women"
  },
  {
    "id": 3,
    "name": "Kids"
  }
]
[
  {
    "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" : []
    }
]
子类别JSON 我通过访问API并在$scope.subcategory列表中对其排序来获取此JSON

[
  {
    "id": 1,
    "name": "Men"
  },
  {
    "id": 2,
    "name": "Women"
  },
  {
    "id": 3,
    "name": "Kids"
  }
]
[
  {
    "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" : []
    }
]
我需要以以下格式设计此文件

[
  {
    "id": 1,
    "name": "Men"
  },
  {
    "id": 2,
    "name": "Women"
  },
  {
    "id": 3,
    "name": "Kids"
  }
]
[
  {
    "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);
    });    
});
看看这个逻辑

$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
中检查此逻辑

$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中创建结果。您可以将与结合使用以实现所需的结果:

var类别=[{
“id”:1,
“姓名”:“男子”
},
{
“id”:2,
“姓名”:“妇女”
},
{
“id”:3,
“姓名”:“孩子”
}
];
变量子类别=[{
“id”:1,
“类别识别号”:1,
“名称”:“鞋类”
},
{
“id”:2,
“类别识别号”:2,
“名称”:“鞋类”
},
{
“id”:3,
“类别识别号”:1,
“名称”:“布”
}
];
var result=categories.map(cat=>{
返回{
类别id:cat.id,
categoryName:cat.name,
子类别:子类别
.filter(subc=>subc.category_id==cat.id)
.map(subc=>{
返回{
子类别id:子类别id,
子类别名称:subc.name
};
})
};
});
控制台日志(结果)您可以与结合使用以实现所需的结果:

var类别=[{
“id”:1,
“姓名”:“男子”
},
{
“id”:2,
“姓名”:“妇女”
},
{
“id”:3,
“姓名”:“孩子”
}
];
变量子类别=[{
“id”:1,
“类别识别号”:1,
“名称”:“鞋类”
},
{
“id”:2,
“类别识别号”:2,
“名称”:“鞋类”
},
{
“id”:3,
“类别识别号”:1,
“名称”:“布”
}
];
var result=categories.map(cat=>{
返回{
类别id:cat.id,
categoryName:cat.name,
子类别:子类别
.filter(subc=>subc.category_id==cat.id)
.map(subc=>{
返回{
子类别id:子类别id,
子类别名称:subc.name
};
})
};
});
控制台日志(结果)
var categoryList=[{
“id”:1,
“姓名”:“男子”
}, {
“id”:2,
“姓名”:“妇女”
}, {
“id”:3,
“姓名”:“孩子”
}];
变量子类别列表=[{
“id”:1,
“类别识别号”:1,
“名称”:“鞋类”
}, {
“id”:2,
“类别识别号”:2,
“名称”:“鞋类”
}, {
“id”:3,
“类别识别号”:1,
“名称”:“布”
}];
var finalJson=[];
对于(变量i=0;i
纯Javascript解决您的问题,您可以替换为 那么

var类别列表=[{
“id”:1,
“姓名”:“男子”
}, {
“id”:2,
“姓名”:“妇女”
}, {
“id”:3,
“姓名”:“孩子”
}];
变量子类别列表=[{
“id”:1,
“类别识别号”:1,
“名称”:“鞋类”
}, {
“id”:2,
“类别识别号”:2,
“名称”:“鞋类”
}, {
“id”:3,
“类别识别号”:1,
“名称”:“布”
}];
var finalJson=[];
对于(变量i=0;i
纯Javascript解决您的问题,您可以替换为 那么


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

const类别列表=[
{
“id”:1,
“姓名”:“男子”
},
{
“id”:2,
“姓名”:“妇女”
},
{
“id”:3,
“姓名”:“孩子”
}
];
常量子类别列表=[
{
“id”:1,
“类别\u id”:1,
“名称”:“鞋类”
},
{
“id”:2,
“类别识别号”:2,
“名称”:“鞋类”
},
{
“id”:3,
“类别识别号”:1,
“名称”:“布”
}
];
const mergeCategoryList=(categoryList,subCategoryList)=>{
//将categoryList转换为以categoryId为键的对象
const categoryById={};
类别列表forEach((类别)=>{
categoryById[category.id]={
categoryName:category.name,
类别id:category.id,
子类别:[]
};
});
//添加子类别
子类别列表。forEach((子类别)=>{
常量格式化子类别={
subCategory id:subCategory.id,
子类别名称:subCategory.name
};
categoryById[subCategory.category\u id]。subCategory.push(formattedSubCategory);
});
//将categoryById转换为所需格式
返回Object.values(categoryById);
};

log(mergeCategoryList(categoryList,subCategoryList))这应该可以做到。不像31piy那样干净(哇!),但效率更高。(O(N+M)相对于O(N*M))

cons