JavaScript - Append Array at Specific Position of Another Array Last Updated : 28 Nov, 2024 Comments Improve Suggest changes Like Article Like Report Here are the different approaches to appending an array at a specific position within another array in JavaScript.Using splice() Method - Most UsedThe splice() method is the most simple approach to insert elements at a specific position. It inserts elements of a2 at index 2 of a1 without removing any elements. JavaScript let a1 = [1, 2, 6]; let a2 = [3, 4, 5]; a1.splice(2, 0, ...a2); console.log(a1); Output[ 1, 2, 3, 4, 5, 6 ] Using Slice and Spread OperatorIt creates a new array without modifying the original. It splits a1 at a specific point and joins it with a2 using the spread operator. JavaScript let a1 = [1, 2, 6]; let a2 = [3, 4, 5]; let pos = 2; let res = [...a1.slice(0, pos), ...a2, ...a1.slice(pos)]; console.log(res); Output[ 1, 2, 3, 4, 5, 6 ] Using for LoopIt inserts items into a specific position in the array. It goes through each element of a2 and adds it to a1 at the given position using the splice() method. JavaScript let a1 = [1, 2, 6]; let a2 = [3, 4, 5]; let pos = 2; for (let i = 0; i < a2.length; i++) { a1.splice(pos + i, 0, a2[i]); } console.log(a1); Output[ 1, 2, 3, 4, 5, 6 ] Using concat() Method with Manual Position HandlingIt combines slice() and concat() to keep the original arrays unchanged. It slices a1 at the chosen position and uses concat() to join it with a2. JavaScript let a1 = [1, 2, 6]; let a2 = [3, 4, 5]; let pos = 2; let res = a1.slice(0, pos).concat(a2).concat(a1.slice(pos)); console.log(res); Output[ 1, 2, 3, 4, 5, 6 ] Using reduce() MethodIt’s a functional way to insert arrays. It uses reduce() to create a new array and adds a2 when it reaches the specified position. JavaScript let a1 = [1, 2, 6]; let a2 = [3, 4, 5]; let pos = 2; let res = a1.reduce((acc, val, index) => { if (index === pos) acc.push(...a2); acc.push(val); return acc; }, []); console.log(res); Output[ 1, 2, 3, 4, 5, 6 ] Using Array.prototype.flat() with Manual ManipulationIt merges the arrays and flattens them for easy insertion. It uses slicing and flat() to join the arrays into a single array. JavaScript let a1 = [1, 2, 6]; let a2 = [3, 4, 5]; let pos = 2; let res = [a1.slice(0, pos), a2, a1.slice(pos)].flat(); console.log(res); Output[ 1, 2, 3, 4, 5, 6 ] Which Approach to Use?splice() Method: Best when you want to modify the original array directly and quickly insert elements.Slice and Spread Operator: Ideal for creating a new array without changing the original arrays.for Loop: Useful when you need control and flexibility over how elements are inserted.concat() Method: Great for immutability when you need to insert elements without modifying the original arrays.reduce() Method: Suitable if you prefer a functional approach.flat() Method: Helpful when you prefer concise, clean code with slicing and flattening. Comment More infoAdvertise with us Next Article JavaScript - Append Array at Specific Position of Another Array S souravsharma098 Follow Improve Article Tags : JavaScript Web Technologies javascript-array Similar Reads JavaScript - Append Array At The End Of Another Array Here are the different approaches to append an array at the end of another array in JavaScriptpush() Method with Spread Operator - Most UsedIt uses push() combined with the spread operator to append elements of a2 to a1.JavaScriptlet a1 = [ 1, 2, 3 ]; let a2 = [ 4, 5, 6 ]; a1.push(...a2); console.lo 3 min read JavaScript - Append Array At The Beginning Of Another Array Here are the various approaches to append an array at the beginning of another array in JavaScriptUsing unshift() Method - Most CommonUses the unshift() method to add elements of a2 at the beginning of a1 using the spread operator.JavaScriptlet a1 = [ 4, 5, 6 ]; let a2 = [ 1, 2, 3 ]; a1.unshift(...a 2 min read JavaScript - Add Array to Array of Array Here are the different methods to Array to an Array of Array (Multidimensional Array) in JavaScript1. Using push() MethodThe push() method is one of the most common ways to add an array to an array of arrays. It adds a new element (in this case, an array) to the end of the array.JavaScriptlet mat = 4 min read How to move an array element from one array position to another in JavaScript? In JavaScript, we can access an array element as in other programming languages like C, C++, Java, etc. Also, there is a method called splice() in JavaScript, by which an array can be removed or replaced by another element for an index. So to move an array element from one array position to another 5 min read JavaScript - Insert Elements at a Given Position in an JS Array To insert an element at a specific position in a JavaScript array, the JS splice() method is used. JavaScriptlet a = [10, 20, 30, 40]; let e = 50; let i = 2; a.splice(i - 1, 0, e); console.log(a);Output[ 10, 50, 20, 30, 40 ] Table of ContentUsing built-in MethodWriting Your Own MethodUsing built-in 1 min read JavaScript- Append in Array These are the following ways to append an element to the JS array: 1. Using Array.push() MethodJavaScript array.push() Method is used to add one or more elements to the end of the array.JavaScriptlet a = [ 10, 20, 30, 40, 50 ]; a.push(90); console.log(a);Output[ 10, 20, 30, 40, 50, 90 ] 2. Using Spr 2 min read How to Store an Object Inside an Array in JavaScript ? Storing an object inside an array in JavScript involves placing the object as an element within the array. The array becomes a collection of objects, allowing for convenient organization and manipulation of multiple data structures in a single container. Following are the approaches through which it 3 min read JavaScript - Inserting Multiple Items at Specific Index in JS Array We can insert an item into Array at a specific index using the JavaScript Array splice method. This method removes the elements from the mentioned index and add items at that specific position.1. Using array.splice() MethodJavaScript array.splice() Method is an inbuilt method in JavaScript that is u 3 min read JavaScript- Add an Object to JS Array In JavaScript, arrays are used to store multiple values in a single variable, and objects are collections of properties and values. Sometimes, we may need to add an object to an array to manage data more effectively. These are the following ways to add an object to a JS array: Using JavaScript Array 4 min read Extend existing JS array with Another Array To extend an array with another without creating a new array we can use the JavaScript array.push() method. This method extend the array by adding the elements at the end. These are the following methods: 1. Using array.push() with Spread operator JavaScript array.push() method adds one or more elem 2 min read Like