我有
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循环,但我不知道该怎么做。
答案 0 :(得分:12)
您可以使用Array.prototype.forEach()
进行数组迭代。
forEach()
方法每个数组元素执行一次提供的函数。
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>');
&#13;
答案 1 :(得分:11)
您可以使用两个for循环:
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);
答案 2 :(得分:6)
基于@Nina Scholz的摘录
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>');
答案 3 :(得分:5)
仍然有循环选项:
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);
答案 4 :(得分:3)
array1.length
和array2.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);
&#13;
答案 5 :(得分:0)
尝试(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);
}
答案 6 :(得分:0)
由于该语言未内置该函数,因此它是一个简单函数,其签名与内置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 { "\($0.1) \($0.0)" }
甚至:
cartesianProduct(1...5, ["one","two","three","four","five"])
.map { "\($0.1) \($0.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 { $0.0 == $0.1 }