简体   繁体   中英

Push array of elements inside another array at a index in javascript

I need to push array of elements inside another array at a particular index in javascript without using spread operator and without using another array.

Input:

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

Expected Output:

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

I tried using splice and apply function like below.

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

This code gives expected output but also updating the elements in firstArray before updating the secondArray.

Is there any better way to achieve the expected output without updating the elements in firstArray?

Edit 1: I am trying to achieve this without new array because the size of the firstArray and secondArray are huge. Thus feeling it might create unwanted memory allocation for new array to just push elements into another array. Avoiding spread operator as it is not supported in IE browsers Edit 2: Removed without using loops condition.

You've said your reason for the constraints is:

...because the size of the firstArray and secondArray are huge. Thus feeling it might create unwanted memory allocation for new array to just push elements into another array.

That being the case, you want to avoid that .splice(0, 0, 3, 0) on firstArray , since it requires moving everything in firstArray over to make room for the elements.

Your memory issue doesn't explain the prohibition on using a loop (after all, splice uses a loop) and copyWithin (also a loop) combined with a loop to fill in the new elements would be a clean, simple, and not memory-intensive way to solve the problem. So:

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

Live Example:

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

If the while loop seems a bit opaque to you, you can also write the loop part of that like this:

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

Live Example:

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

Note that copyWithin is probably not supported by IE11, but is easily polyfilled.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM