如何使用一个forEach循环遍历两个不同的数组?

For*_*ton 0 javascript arrays foreach loops function

我希望能够使用一个forEach循环将这两个数组的值记录到控制台上。到目前为止,我只迭代第一个,因为这就是我所知道的全部方法!这可能吗?

var array1 = [1, 2, 3, 4];
var array2 = [5, 6, 7, 8];

array1.forEach(function(item){
    console.log(item)
});
Run Code Online (Sandbox Code Playgroud)

Nen*_*car 5

如果两个数组的长度相同,则可以index用来记录其他数组中的元素。

var array1 = [1, 2, 3, 4];
var array2 = [5, 6, 7, 8];

array1.forEach(function(item, index){
  console.log(item, array2[index])
});
Run Code Online (Sandbox Code Playgroud)