Loop through an two arrays simultaneously and pass a first element of each array to the function at a time using angular js [new to angular]

后端 未结 3 1416
灰色年华
灰色年华 2021-01-16 12:08
vm.array1.push(content1);
vm.array2.push(content2);

I have a above two arrays with a data of objects pushed at each time and the data modal of each

相关标签:
3条回答
  • 2021-01-16 12:54

    At the simplest you can do following. (This solution will modify your arrays)

    do{
       vm.save(vm.array1.shift(), vm.array2.shift()){
       //save functionality success by calling API
       }
    }while(vm.array1.length>0)
    
    0 讨论(0)
  • 2021-01-16 13:04

    Plz check may this will be work:-

    for(var i=0;i<=vm.array1.length;i++)
    {
        for(var j=0;j<=vm.array2.length;j++)
        {
            if(i==j)
            vm.save(vm.array1[i].content1,vm.array2[j].content2){
            //save functionality success by calling API
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-16 13:13

    I'm guessing array1 and array2 are of same length. This should work.

    var vm = {
      save: function(a, b) {
        console.log(a, b)
      }
    };
    vm.array1 = [{
      id: 1
    }, {
      id: 2
    }, {
      id: 3
    }];
    vm.array2 = [{
      id: 4
    }, {
      id: 5
    }, {
      id: 6
    }];
    
    vm.array1.forEach(function(a1, i) {
      vm.save(a1, vm.array2[i]);
    });

    0 讨论(0)
提交回复
热议问题