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?
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?
Share Improve this question asked Jun 20, 2017 at 6:01 HKI345HKI345 3133 gold badges8 silver badges24 bronze badges 5- do you mean to call a function inside loop? – brk Commented Jun 20, 2017 at 6:04
- No...function should be called outside the loop@brk – HKI345 Commented Jun 20, 2017 at 6:05
- ok then when will the function called with second elements? – brk Commented Jun 20, 2017 at 6:07
- so,the function should be looped? – HKI345 Commented Jun 20, 2017 at 6:08
- Assuming they have the same length, loop over one of the arrays and just pass the items at the corresponding indexes. – Evan Trimboli Commented Jun 20, 2017 at 6:10
3 Answers
Reset to default 2I'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]);
});
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)
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
}
}
}