【问题标题】:fill a array with foreach in foreach javascript (angular)在 foreach javascript 中用 foreach 填充数组(角度)
【发布时间】:2015-05-28 16:09:32
【问题描述】:

我有类别。每个类别都有项目。现在我从 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: Cannot read property 'push' of undefined

我必须如何以正确的方式做到这一点?我想要一个包含所有项目的类别对象内的对象

【问题讨论】:

  • 在哪一行出错?
  • $scope.delivery_items[k][i].push(val);

标签: javascript angularjs foreach


【解决方案1】:

我认为问题在于 $scope.delivery_items[k][i].push(val); 中的 k 将其更改为 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"
    ]
}

【讨论】:

  • TypeError: 无法读取该行上未定义的属性 '1' ..
  • 我没有得到这个解决方案。项目也是其中包含多个数据的对象
  • 你之前使用数组的方式很时髦,它的位置不正确或看起来不正确,这样你就可以创建一个有组织的对象;使用类别名称作为对象的键,然后填充项目名称作为包含所述项目信息的对象的键。查看示例对象的答案。
  • 现在我得到:ReferenceError:item_category_id 未使用您的代码定义:这条线 angular.forEach($scope.items.filter(check_item_catagory(item_category_id,v.item_category_id)),函数 (val,键){
  • 对,抱歉弄错了,过滤器很棘手,要让回调接受一个额外的值(类别 id),您需要创建一个接受值的函数(类别 id ) 然后返回实际的回调,该回调也接受一个值(数组中的项目),然后将该项目的 id 与之前提供的类别 id 进行比较。
【解决方案2】:

删除“[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);
                    }
                });
            }
        });
    }

或者去掉'push':

    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;
                    }
                });
            }
        });
    }

【讨论】:

  • a 已经完成了这两项操作,当我删除 push “TypeError: Cannot set property '9' of undefined”时,当我在没有 i 的情况下 push “TypeError: undefined is not a function”时
猜你喜欢
  • 2018-12-11
  • 1970-01-01
  • 1970-01-01
  • 2012-01-16
  • 1970-01-01
  • 2016-12-15
  • 2017-07-29
  • 1970-01-01
  • 2012-08-17
相关资源
最近更新 更多