erstellen von JSON-array mit Schlüssel mit AngularJS

erstellen Sie eine json array-Struktur mit key mit angularjs. Ich habe keine Idee, wie push-Daten zum erstellen von json-array-Struktur mit key. Ich möchte json-array mit dem unten json-Daten.

$scope.category = [{"id": 20, "name": "vegetable"},
    {"id": 30, "name": "fruits"}];

$scope.data = [
    { "id" : 1,"name" : "tomato", "categoryId" : 20},
    { "id" : 2,"name" : "potato", "categoryId" : 20},
    { "id" : 3,"name" : "orange", "categoryId" : 30},
    { "id" : 4,"name" : "apple", "categoryId" : 30},
    { "id" : 4,"name" : "onion", "categoryId" : 20}];

for(var i=0; i<$scope.category.length; i++) {
    for(var j=0; j<$scope.data.length; j++) {
        if($scope.category[i].id === $scope.data[j].categoryId) {

        }
    }
}

Möchte ich eine Ausgabe wie diese:

  {
    "vegetable" : [
        { "id" : 1, "name" : "tomato"},
        { "id" : 2, "name" : "potato"},
        { "id" : 3, "name" : "onion"},
    ],
    "fruits" : [
        { "id" : 3, "name" : "orange"},
        { "id" : 4, "name" : "apple"}
    ]
 }

InformationsquelleAutor Satheesh Natarajan | 2015-05-05

Schreibe einen Kommentar