javascript - For inside other for in javascript 无法正常工作

标签 javascript arrays for-loop

我有这个代码

for (var i = 0; i < vm.items.users.data.length; i++) {
      var user = vm.items.users.data[i];
      user.goals_by_brands = [];
      var brands = [];
      vm.items.brands.data.forEach( function(element, index) {
        brands.push(element);
      });
      console.log("brands", brands)
      console.log("vm.items.brands.data", vm.items.brands.data)
      brands.forEach( function(brand) {
        brand.goals_by_months = [];
        brand.user = user;
        constants.MONTHS.forEach( function(month) {
          brand.goals_by_months.push({goals: [], total_month_goal: 0, total_month_accumulated: 0});
        });
        user.goals_by_brands.push(brand);
      });

}

我写了这行:

var brands = [];
      vm.items.brands.data.forEach( function(element, index) {
        brands.push(element);
      });

但我也尝试过克隆数组(slice() 函数),它的作用是相同的。

在brands数组和vm.items.brands.data数组中,出现了相同的情况; vm.items.users.data 数组中的最后一个用户。

不知道为什么..

我想这样做:

  • 我有用户数组。 -> vm.items.users.data
  • 我有很多品牌。 -> vm.items.brands.data
  • 我有几个月的数组。 -> 常量.MONTHS

我想向每个品牌添加包含此对象的数组 -> {goals: [],total_month_goal: 0,total_month_accumulated: 0} 12 次(每月一次)。 然后这个品牌,我想添加到每个用户 ->

[
      {
        id: "user1",
        name: "user1",
        goals_by_brands: [
          {
            id: "brand1",
            name: "brand1",
            goals_by_months: [
              {goals: [], total_month_goal: 0, total_month_accumulated: 0},
              {goals: [], total_month_goal: 0, total_month_accumulated: 0},
              {goals: [], total_month_goal: 0, total_month_accumulated: 0},
              {goals: [], total_month_goal: 0, total_month_accumulated: 0},
              {goals: [], total_month_goal: 0, total_month_accumulated: 0},
              {goals: [], total_month_goal: 0, total_month_accumulated: 0},
              {goals: [], total_month_goal: 0, total_month_accumulated: 0},
              {goals: [], total_month_goal: 0, total_month_accumulated: 0},
              {goals: [], total_month_goal: 0, total_month_accumulated: 0},
              {goals: [], total_month_goal: 0, total_month_accumulated: 0},
              {goals: [], total_month_goal: 0, total_month_accumulated: 0},
              {goals: [], total_month_goal: 0, total_month_accumulated: 0}
            ]
          }
        ]

    }]

所以我想将用户添加到 goal_by_brands 的每个品牌对象中。

对不起我的英语。

最佳答案

因此,您的代码的问题在于您拥有“品牌”引用,而不是品牌的副本。由于您正在更改主要引用,因此品牌会不断被覆盖,因此请在循环中进行此更改。

var brands = []; 
vm.items.brands.data.forEach( function(element, index) { 
brands.push(element); //This pushes the reference which remains the same.
});

将此推送线更改为

var newBrand = JSON.parse(JSON.stringify(element))) //Make a deep copy of the element
brands.push(newBrand)

关于javascript - For inside other for in javascript 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42505542/

相关文章:

javascript - 适合屏幕底部/顶部的菜单

PHP, in_array, 0 值

PHP 可遍历类型提示

javascript - 合并两个对象及其内部的数组

java - 按每个数字与其索引的乘积对整数数组进行排序?‽

xcode - 在 For In Loop 中解包 Optional 值时意外发现 nil

mysql - MySQL 中的 For Loop,循环遍历表并将其应用到 where 语句

javascript - HTML数据集-如何放置对象?数据-x={}

javascript - 动态表创建者或 <td> <tr> 创建者

javascript - 为什么函数输出总是5?