用foreach javascript(angular)填充foreach数组

时间:2015-05-28 16:09:33

标签: javascript angularjs foreach

我有类别。每个类别都有项目。现在我从api返回一个类别列表和一个项目列表。

现在在角度我想把它变成1个对象。我做了以下事情:

        if ($scope.categories_norm) {
            $scope.delivery_items = [];

            //get categories
            angular.forEach(data.item_categories.normal_categories, function (v, k) {
                //push categories
                $scope.delivery_items.push(v);

                //push items for categories
                var i = 0;
                if ($scope.items) {
                    angular.forEach($scope.items, function (val, key) {
                        i++;
                        if (val.item_category_id == v.item_category_id) {
                        //console.log();
                            $scope.delivery_items[k][i].push(val);
                        }
                    });
                }
            });
        }
由于某些原因,我无法填写此内容。它返回错误:TypeError:无法读取未定义的属性'push'

我该如何以正确的方式做到这一点?我希望每个类别对象中的对象都包含所有项目

2 个答案:

答案 0 :(得分:0)

删除' [i]'

    if ($scope.categories_norm) {
        $scope.delivery_items = [];

        //get categories
        angular.forEach(data.item_categories.normal_categories, function (v, k) {
            //push categories
            $scope.delivery_items.push(v);

            //push items for categories
            var i = 0;
            if ($scope.items) {
                angular.forEach($scope.items, function (val, key) {
                    i++;
                    if (val.item_category_id == v.item_category_id) {
                    //console.log();
                        $scope.delivery_items[k].push(val);
                    }
                });
            }
        });
    }

或删除'推送':

    if ($scope.categories_norm) {
        $scope.delivery_items = [];

        //get categories
        angular.forEach(data.item_categories.normal_categories, function (v, k) {
            //push categories
            $scope.delivery_items.push(v);

            //push items for categories
            var i = 0;
            if ($scope.items) {
                angular.forEach($scope.items, function (val, key) {
                    i++;
                    if (val.item_category_id == v.item_category_id) {
                    //console.log();
                        $scope.delivery_items[k][i] = val;
                    }
                });
            }
        });
    }

答案 1 :(得分:0)

我认为问题是k $scope.delivery_items[k][i].push(val);将其更改为v,看看会发生什么。

编辑:

好的,在再次查看您的问题后,我认为这可能会更好:

if ($scope.categories_norm) {
        $scope.delivery_items = {}; // create empty object
        //get categories
        angular.forEach(data.item_categories.normal_categories, function (v, k) {
            var catagery = v.category_name; // create cataogry variable scoped to this block
            $scope.delivery_items[category] = {}; //create empty category object using category name as the key
            if ($scope.items) {
                angular.forEach($scope.items.filter(check_item_category(v.item_category_id)), function (val, key) {
                    $scope.delivery_items[category][category_info] = v; // comment out or omit this line if you don't want category information included within the category
                    $scope.delivery_items[category][val.item_name] = val; // set item object to it's corosponding name
            });
        };
    });
}

function check_item_category (category_id) {
    return function(item) {
        return item.item_category_id == category_id
    }
}

你仍然需要填写一些内容(即如何获取类别和项目名称),但这应该是你想要的。

编辑:我转而使用过滤器,我只是没有看到每个项目的ID检查,我认为使用过滤器会更清洁。

编辑:

它应该以这样的对象结束:

{
    "category_1" : {
        "category_info" : category_object,
        "item_1" : item_1_object,
        "item_2" : item_2_object
    }
    "category_2" : {
        "category_info" : category_object,
        "item_1" : item_1_object,
        "item_2" : item_2_object,
        "item_3" : item_3_object,
    }
    "category_3" : {
        "category_info" : category_object,
        "item_1" : item_1_object,
        "item_2" : item_2_object,
        "item_3" : item_3_object,
    }
}

编辑: 好的,最后一次,我再次看了一遍,意识到这可以更快(减少迭代次数$scope.items),同时将删除重复的数据(项目的名称是在密钥和项目对象中)。

请注意,如果您使用此解决方案,则不会将类别对象与类别一起存储

if ($scope.categories_norm) {
        $scope.delivery_items = get_items_sorted_by_category(data.item_categories.normal_categories,$scope.items) // create empty object
    });
}

function get_items_sorted_by_category(categories,items) {
    var filled_object = {}
    for(var category in categories){
        //will need to change how you set the catagory name as I don't know how this object looks
        filled_object[category.name] = items.filter(check_item_category(category.item_category_id)) // will filter out any objects that don't have the correct caragory ID
    };
    return filled_object;
}

function check_item_category (category_id) {
    return function(item) {
        return item.item_category_id == category_id
    }
}

你应该得到一个如下所示的对象:

{
    "category_1" : [
        "item_1", 
        "item_2", 
        "item_3"
    ]
    "category_2" : [
        "item_1",
        "item_2",
        "item_3"
    ]
    "category_3" : [        
        "item_1",
        "item_2",
        "item_3"
    ]
}