Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
276 views
in Technique[技术] by (71.8m points)

javascript - 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]

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 array looks like this

vm.array1=[object1,object2,object3]
vm.array2=[object1,object2,object3]

I need to pass only object of first element of array1 with the object of first element of array2 to the function.

vm.save(a,b){
//save functionality success by calling API
}

variables a,b should contain only the first elements of both the array simultaneously followed by second elements then third...

How can i pass only objects but not arrays to the function using angularJS?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...