var array1 = ["a", "b", "c", "d"],
array2 = [1, 2],
result = [array1, array2]
.reduce((r, a) => (a.forEach((a, i) => (r[i] = r[i] || []).push(a)), r), [])
.reduce((a, b) => a.concat(b));
console.log(result);
Eğer kurucuların atama
const interleave = ([x,...xs], [y,...ys]) =>
x === undefined && y === undefined
? []
: x === undefined
? [y] .concat (ys)
: y === undefined
? [x] .concat (xs)
: [x, y] .concat (interleave (xs, ys))
console.log (interleave ([0, 2, 4, 6], [1, 3, 5])) // [ 0 1 2 3 4 5 6 ]
console.log (interleave ([0, 2, 4], [1, 3, 5, 7])) // [ 0 1 2 3 4 5 7 ]
console.log (interleave ([0, 2, 4], [])) // [ 0 2 4 ]
console.log (interleave ([], [1, 3, 5, 7])) // [ 1 3 5 7 ]
console.log (interleave ([], [])) // [ ]
kullanarak bunu yapabilirsiniz başka bir yolu Ve işte uygun bir kuyruk çağrısı kullanıyor
const interleave = ([x,...xs], [y,...ys], acc = []) =>
x === undefined && y === undefined
? acc
: x === undefined
? acc.concat (y, ys)
: y === undefined
? acc.concat (x, xs)
: interleave (xs, ys, acc.concat ([x, y]))
console.log (interleave ([0, 2, 4, 6], [1, 3, 5])) // [ 0 1 2 3 4 5 6 ]
console.log (interleave ([0, 2, 4], [1, 3, 5, 7])) // [ 0 1 2 3 4 5 7 ]
console.log (interleave ([0, 2, 4], [])) // [ 0 2 4 ]
console.log (interleave ([], [1, 3, 5, 7])) // [ 1 3 5 7 ]
console.log (interleave ([], [])) // [ ]
Ve yine kurucuların atama
const interleave = (xs, ys, acc = []) =>
xs.length === 0 && ys.length === 0
? acc
: xs.length === 0
? acc.concat (ys)
: ys.length === 0
? acc.concat (xs)
: interleave (xs.slice (1), ys.slice (1), acc.concat ([xs[0], ys[0]]))
console.log (interleave ([0, 2, 4, 6], [1, 3, 5])) // [ 0 1 2 3 4 5 6 ]
console.log (interleave ([0, 2, 4], [1, 3, 5, 7])) // [ 0 1 2 3 4 5 7 ]
console.log (interleave ([0, 2, 4], [])) // [ 0 2 4 ]
console.log (interleave ([], [1, 3, 5, 7])) // [ 1 3 5 7 ]
console.log (interleave ([], [])) // [ ]
Veya kuyruk çağrısı ve endeks bir arada kullanmadan - bu hızlı diğer parçacıkları ile karşılaştırıldığında ise ben en az tahsisat yaptıkça sağlandı, en az ara değer yarattı ve dolayısıyla daha az hesaplama sonrası çöp toplama - kazan/kazan/
const interleave = (xs, ys, i = 0, acc = []) =>
xs.length === i && ys.length === i
? acc
: xs.length === i
? acc.concat (ys.slice (i))
: ys.length === i
? acc.concat (xs.slice (i))
: interleave (xs, ys, i + 1, acc.concat ([xs[i], ys[i]]))
console.log (interleave ([0, 2, 4, 6], [1, 3, 5])) // [ 0 1 2 3 4 5 6 ]
console.log (interleave ([0, 2, 4], [1, 3, 5, 7])) // [ 0 1 2 3 4 5 7 ]
console.log (interleave ([0, 2, 4], [])) // [ 0 2 4 ]
console.log (interleave ([], [1, 3, 5, 7])) // [ 1 3 5 7 ]
console.log (interleave ([], [])) // [ ]
keskin i
ve acc
özel API sızdırılmış olduğunu fark edecektir kazanmak - bu kolayca bir loop
yardımcı kullanarak giderildiği olabilir; Bu teknik ayrıca bir de interleave
yığını güvenli hale getirir not olmayan kuyruk-call-optimize çevre yukarıda kıyasla
Yani, biz iki daha kazanır olsun, bu kazan/kazan/kazan/kazan yapma/kazan
Eğer "* * bunu yapmanın en iyi yolu nedir" ile ne
const recur = (...values) =>
({ type: recur, values })
const loop = f =>
{
let acc = f()
while (acc && acc.type === recur)
acc = f (...acc.values)
return acc
}
const interleave = (xs, ys) =>
loop ((i = 0, acc = []) =>
xs.length === i && ys.length === i
? acc
: xs.length === i
? acc.concat (ys.slice (i))
: ys.length === i
? acc.concat (xs.slice (i))
: recur (i + 1, acc.concat ([xs[i], ys[i]])))
console.log (interleave ([0, 2, 4, 6], [1, 3, 5])) // [ 0 1 2 3 4 5 6 ]
console.log (interleave ([0, 2, 4], [1, 3, 5, 7])) // [ 0 1 2 3 4 5 7 ]
console.log (interleave ([0, 2, 4], [])) // [ 0 2 4 ]
console.log (interleave ([], [1, 3, 5, 7])) // [ 1 3 5 7 ]
console.log (interleave ([], [])) // [ ]
demek? Beklenen sonuç nedir? "birleşme" oldukça geniş bir kavramdır. –
Son sonuç nasıl görünmeli? – noitse
değişken değerleri ile dizi1'den 1, sonra dizi2'den biri gibi mi demek istiyorsunuz? – schylake