1
var arr1 = [1,2,3,4,5];
var arr2 = ["a","b","c","d","e"];

Let's assume that I want to create a list like

1 a
2 b
3 c
4 d
5 e

by using template literal.

let x;
x = document.createElement('li');
x.innerHTML += `<span>${<arr1 index>}</span> <span>${<arr2 index>}</span>`

How can I do that ? Can we use forEach for two arrays in same time ?

trincot
  • 317,000
  • 35
  • 244
  • 286

4 Answers4

0

You could simply use a for() loop instead:

const max = Math.max(arrA.length, arrB.length)

for (let i = 0; i < max; i++) {
  const objA = arrA[i],
        objB = arrB[i]

  if ('undefined' !== typeof objA) {
    console.log({ objA })
  }
  if ('undefined' !== typeof objB) {
    console.log({ objB })
  }
}
anthumchris
  • 8,245
  • 2
  • 28
  • 53
0

This would be more like flatten(zip(arr1, arr2)). There is no built-in zip though you can very easily make it and you can see Array.flat here: MDN: Array.flat.

const arr1 = [1, 2, 3, 4, 5];
const arr2 = ["a", "b", "c", "d", "e"];

const flatten = arr => arr.flat();
const zip = (a, b) => a.map((e, idx) => [e, b[idx]]);

const arr3 = flatten(zip(arr1, arr2));

console.log(arr3);
zero298
  • 25,467
  • 10
  • 75
  • 100
0

The answer is "kind of." What you can do is loop through one array with a forEach method, and use the optional argument index to get the value of the second array as well. Something like this:

var arr1 = [1,2,3,4,5];
var arr2 = ["a","b","c","d","e"];

arr1.forEach((value, index) => {
    console.log(value);
    console.log(arr2[index])
})

But if the data in the two arrays are at all related, you'd want to put the data in the same object, like this:

var arr = [
    {
        num: 1,
        letter: "a"
    },
    {
        num: 2,
        letter: "b"
    },
    {
        num: 3,
        letter: "c"
    }
];

arr.forEach(value => {
    console.log(value.num);
    console.log(value.letter);
})

Or you would want to use a regular for loop

rook218
  • 644
  • 7
  • 20
-1

There is no real magic here. You use an index variable, and let it increment:

var arr1 = [1,2,3,4,5];
var arr2 = ["a","b","c","d","e"];

let ul = document.querySelector("ul");

for (let i = 0; i < arr1.length; i++) {
    let li = document.createElement('li');
    for (let val of [arr1[i], arr2[i]]) {
        let span = document.createElement('span');
        span.textContent = val;
        li.appendChild(span);
    }
    ul.appendChild(li);
}
<ul></ul>

There are of course other ways to loop, like with forEach, but it comes down to the same principle.

BTW, don't use string literals (template literals) for combining HTML with content, as you might have < or & characters in the content, which really should be escaped. In some cases, not escaping those may lead to unexpected side effects. By creating the elements with createElement and assigning content to their textContent or innerText properties, you avoid those potential issues. Some libraries make it possible to do this with less code, in a more functional way.

As to the initial data: in object oriented languages, like JavaScript, it is better practice to put related values together in one object. In the example, 1 and "a" apparently have a connection, so -- if possible -- you should define the initial data structure as something like this:

var data = [
    { value: 1, name: "a" },
    { value: 2, name: "b" },
    { value: 3, name: "c" },
    { value: 4, name: "d" },
    { value: 5, name: "e" }
];
trincot
  • 317,000
  • 35
  • 244
  • 286