Question: I have

```
array1 = [1,2,3,4,5];
array2 = ["one","two","three","four","five"];

```

I want to get `array3` where all elements of `array1` with first (and others) element of `array2` and etc.

For example:

```
array3 = ["one 1", "two 1", "three 1", "four 1", "five 1", "one 2", "two 2", "three 2", "four 2", "five 2"...]

```

I understand that I need to use for loop but I don't know how to do it.

Answer: 
You can use this method when `array1.length` and `array2.length` are equal.

```js
var array1 = [1, 2, 3, 4, 5];
var array2 = ["one", "two", "three", "four", "five"];
var length = array1.length;
var array3 = new Array(Math.pow(length, 2)).fill(0).map((v, i) => array2[i % length] + ' ' + array1[i / length << 0]);

document.body.textContent = JSON.stringify(array3);
```
Since this is not built into the language, here's a simple function with a similar signature to the built-in `zip`:

```
func cartesianProduct<Sequence1, Sequence2>(_ sequence1: Sequence1, _ sequence2: Sequence2) -> [(Sequence1.Element, Sequence2.Element)]
    where Sequence1 : Sequence, Sequence2 : Sequence
{
    var result: [(Sequence1.Element, Sequence2.Element)] = .init()
    sequence1.forEach { value1 in
        sequence2.forEach { value2 in
            result.append((value1, value2))
        }
    }
    return result
}

print(Array(zip([1, 2, 3], ["a", "b"]))) // [(1, "a"), (2, "b")]
print(cartesianProduct([1, 2, 3], ["a", "b"])) // [(1, "a"), (1, "b"), (2, "a"), (2, "b"), (3, "a"), (3, "b")]

```

In your case, you could do:

```
cartesianProduct([1,2,3,4,5], ["one","two","three","four","five"])
  .map { "\($0.1) \($0.0)" }

```

or even:

```
cartesianProduct(1...5, ["one","two","three","four","five"])
  .map { "\($0.1) \($0.0)" }

```

both of which will produce the sequence:

```
["one 1", "two 1", "three 1", "four 1", "five 1", "one 2", "two 2", "three 2", "four 2", "five 2", ...]

```

---

Since this is common to do on a collection's elements, I also created these two functional extensions:

```
extension Collection {
    /// O(n^2)
    func pairElementToEveryOtherElement() -> [(Self.Element, Self.Element)] {
        var result = [(Self.Element, Self.Element)]()
        for i in indices {
            var j = index(after: i)
            while j != endIndex {
                result.append((self[i], self[j]))
                j = index(after: j)
            }
        }
        return result
    }

    /// O(n)
    public func pairElementToNeighbors() -> [(Self.Element, Self.Element)] {
        if isEmpty {
            return .init()
        }

        var result: [(Self.Element, Self.Element)] = .init()
        var i = startIndex
        while index(after: i) != endIndex {
            result.append((self[i], self[index(after: i)]))
            i = index(after: i)
        }
        return result
    }
}

```

These can be used like follows:

```
let inefficientHasDuplicatesCheck = myCollection
  .pairElementToEveryOtherElement()
  .contains { $0.0 == $0.1 }

```