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.
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.
Share Improve this question edited Jan 16, 2016 at 14:56 gnat 6,226115 gold badges55 silver badges75 bronze badges asked Jan 16, 2016 at 10:51 frostrockfrostrock 8791 gold badge6 silver badges11 bronze badges 1 |7 Answers
Reset to default 12You can use Array.prototype.forEach()
for the iteration over the arrays.
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>');
You can use two 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);
Yet another way with reduce and map and concat
Snippet based on @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>');
There is still the option with loops:
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);
You can use this method when array1.length
and array2.length
are equal.
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);
Try (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);
}
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 }
zipWith
would work:_.zipWith(array1, array2, function(a,b) { return a + ' ' + b; });
– BlueRaja - Danny Pflughoeft Commented Jan 16, 2016 at 13:20