同时循环遍历两个数组,并使用 angular js 将每个数组的第一个元素一次传递给函数 [angular 的新手] [英] 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]

查看:25
本文介绍了同时循环遍历两个数组,并使用 angular js 将每个数组的第一个元素一次传递给函数 [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]

我只需要将array1的第一个元素的对象和array2的第一个元素的对象传递给函数.

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
}

变量 a,b 应该同时包含两个数组的第一个元素,然后是第二个元素,然后是第三个...

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

如何使用 angularJS 仅将对象而不是数组传递给函数?

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

推荐答案

我猜 array1array2 的长度相同.这应该有效.

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

这篇关于同时循环遍历两个数组,并使用 angular js 将每个数组的第一个元素一次传递给函数 [angular 的新手]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆