簡體   English   中英

在 javascript 中的索引處將元素數組推入另一個數組中

[英]Push array of elements inside another array at a index in javascript

我需要在 javascript 中的特定索引處將元素數組推入另一個數組中,而不使用擴展運算符,也不使用另一個數組。

輸入:

let firstArray = [4,5,6];
let secondArray = [1,2,3,7,8,9];

預期 Output:

console.log(secondArray); //[1,2,3,4,5,6,7,8,9];

我嘗試使用拼接應用function 如下所示。

let firstArray = [4,5,6];
let secondArray = [1,2,3,7,8,9];
firstArray.splice(0, 0, 3, 0); 
secondArray.splice.apply(secondArray, firstArray);

此代碼給出了預期的 output,但也在更新 secondArray 之前更新了 firstArray 中的元素。

有沒有更好的方法來實現預期的 output 而無需更新 firstArray 中的元素?

編輯 1:我試圖在沒有新數組的情況下實現這一點,因為 firstArray 和 secondArray 的大小很大。 因此感覺它可能會為新數組創建不需要的 memory 分配,以便將元素推入另一個數組。 避免傳播運算符,因為它在 IE 瀏覽器中不受支持編輯 2:在不使用循環條件的情況下刪除。

您已經說過限制的原因是:

...因為firstArraysecondArray的大小很大。 因此感覺它可能會為新數組創建不需要的 memory 分配,以便將元素推入另一個數組。

在這種情況下,您要避免在 firstArray 上使用.splice(0, 0, 3, 0) ,因為它需要將firstArray中的所有內容firstArray以為元素騰出空間。

您的 memory 問題並沒有解釋禁止使用循環(畢竟, splice使用循環)和copyWithin (也是循環)結合循環來填充新元素將是一個干凈、簡單且不記憶的問題-密集的方法來解決問題。 所以:

function insert(target, source, index) {
    // Get the count of elements to copy
    let count = source.length;
    if (count) {
        // 1. First, make room by copying elements out of the way
        let targetIndex = index + source.length;
        target.length += count;
        target.copyWithin(targetIndex, index, index + count);
        // 2. Now, fill in the new elements
        while (count-- > 0) {
            target[targetIndex--] = source[count];
        }
    }
    return target;
}

現場示例:

 let firstArray = [4,5,6]; let secondArray = [1,2,3,7,8,9]; function insert(target, source, index) { // Get the count of elements to copy let count = source.length; if (count) { // 1. First, make room by copying elements out of the way let targetIndex = index + source.length; target.length += count; target.copyWithin(targetIndex, index, index + count); // 2. Now, fill in the new elements while (count-- > 0) { target[targetIndex--] = source[count]; } } return target; } insert(secondArray, firstArray, 3); console.log(JSON.stringify(secondArray)); // [1,2,3,4,5,6,7,8,9]; console.log(JSON.stringify(firstArray)); // [4,5,6];

如果while循環對您來說有點不透明,您也可以像這樣編寫循環部分:

for (let index = 0; index < count; ++index) {
    target[targetIndex + index] = source[index];
}

現場示例:

 let firstArray = [4,5,6]; let secondArray = [1,2,3,7,8,9]; function insert(target, source, index) { // Get the count of elements to copy let count = source.length; if (count) { // 1. First, make room by copying elements out of the way let targetIndex = index + source.length; target.length += count; target.copyWithin(targetIndex, index, index + count); // 2. Now, fill in the new elements for (let index = 0; index < count; ++index) { target[targetIndex + index] = source[index]; } } return target; } insert(secondArray, firstArray, 3); console.log(JSON.stringify(secondArray)); // [1,2,3,4,5,6,7,8,9]; console.log(JSON.stringify(firstArray)); // [4,5,6];

請注意,IE11 可能不支持copyWithin ,但很容易填充。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM