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

For-in-other-For-in-javascript无法正常工作,javascript,arrays,for-loop,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(eleme

我有这个密码

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);
      });
但我也尝试过使用克隆数组(slice()函数),它也会这样做

在brands数组和vm.items.brands.data数组中,显示相同;vm.items.users.data数组中的最后一个用户

我不知道为什么

我想这样做:

  • 我有一个用户数组。->vm.items.users.data
  • 我有很多品牌。->vm.items.brands.data
  • 我有个月的数组。->六个月
我想向每个品牌添加带有此对象的数组->{goals:[],total_month_goal:0,total_month_累计: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}
            ]
          }
        ]

    }]
因此,我想将用户添加到每个品牌的目标对象中


对不起,我的英语不好。

所以你的代码的问题是你有“品牌”参考,而不是品牌的副本。由于您正在更改主要参考资料,品牌不断被改写,因此请在您的循环中进行此更改

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)

“它不起作用”-请记住说明您的代码应该做什么以及实际的结果/问题是什么。您想做什么?您能解释一下“在brands数组和vm.items.brands.data数组中,显示的是相同的;vm.items.users.data数组中的最后一个用户。”当我执行for时,在我要添加的用户对象中,出现了vm.items.users.data array@sharjeelahmedd的最后一个用户您的意思是它只接收最后一个用户?你能给我看看JSON最终输出控制台.log(vm.items.users.data)吗
var newBrand = JSON.parse(JSON.stringify(element))) //Make a deep copy of the element
brands.push(newBrand)