如何将两个数组组合为笛卡尔积?

How to combine two arrays as a cartesian product?

我有

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

我想得到 array3 其中 array1 的所有元素与 array2 的第一个(和其他)元素等

例如:

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

我知道我需要使用for循环,但我不知道该怎么做。

您可以使用两个 for-loops:

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

var array3 = [];
for (var i = 0; i < array1.length; i++) {
    for (var j = 0; j < array2.length; j++) {
        array3.push(array2[j] + ' ' + array1[i]);
    }
}

console.log(array3);

您可以使用 Array.prototype.forEach() 对数组进行迭代。

The forEach() method executes a provided function once per array element.

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

array1.forEach(function (a) {
    array2.forEach(function (b) {
        result.push(b + ' ' + a);
    });
});

document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');

reduce and map and concat

的另一种方式

基于的片段

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

var result = array1.reduce(function (acc, cur) {
    return acc.concat(array2.map(function (name) {
        return name + ' ' + cur;
    }));
},[]);

document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');

还有带循环的选项:

var array2 = [1,2,3,4,5],
array1 = ["one","two","three","four","five"],
m = [];
for(var a1 in array1){  
  for(var a2 in array2){
      m.push( array1[a1]+ array2[a2] );    
  }
}
console.log(m);

尝试 (JS)

function myFunction(){
            var F = [1, 2, 3, 4,5];
            var S = ["one", "two", "three", "four", "five"];
            var Result = [];

           var k=0;
            for (var i = 0; i < F.length; i++) {
                for (var j = 0; j < S.length; j++) {
                    Result[k++] = S[j] + " " + F[i];
                }
            }

            console.log(Result);
        }

array1.lengtharray2.length 相等时,您可以使用此方法。

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

由于这不是语言内置的,这里有一个简单的函数,其签名与内置函数相似 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")]

对于你的情况,你可以这样做:

cartesianProduct([1,2,3,4,5], ["one","two","three","four","five"])
  .map { "\([=11=].1) \([=11=].0)" }

甚至:

cartesianProduct(1...5, ["one","two","three","four","five"])
  .map { "\([=12=].1) \([=12=].0)" }

两者都会产生序列:

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

由于这是对集合元素的常见操作,我还创建了这两个功能扩展:

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
    }
}

这些可以像下面这样使用:

let inefficientHasDuplicatesCheck = myCollection
  .pairElementToEveryOtherElement()
  .contains { [=15=].0 == [=15=].1 }