How to Splice an Array Without Mutating the Original Array? Last Updated : 19 Nov, 2024 Comments Improve Suggest changes Like Article Like Report To splice or remove elements without changing the original array, you can use various techniques to create a copy first. Here are the most effective ways to do this1. Using slice() and concat() Methods - Most UsedThe combination of slice() and concat() methods allows you to remove elements from an array without mutating the original by creating a new array. JavaScript const a1 = [1, 2, 3, 4, 5]; // Remove elements at index 1 and 2 const a2 = a1.slice(0, 1).concat(a1.slice(3)); console.log(a2); console.log(a1); Output[1, 4, 5][1, 2, 3, 4, 5]2. Using filter() Method for Conditional RemovalThe filter() method can be used to create a new array which only includes elements that meet a certain condition. JavaScript const a1 = [1, 2, 3, 4, 5]; // Remove elements at index 1 and 2 const a2 = a1.filter((_, index) => index !== 1 && index !== 2); console.log(a2); console.log(a1); Output[1, 4, 5][1, 2, 3, 4, 5]3. Using the Spread Operator and slice() MethodThe spread operator (...) with slice() is a simple way to remove elements without modifying the original array. JavaScript const a1 = [1, 2, 3, 4, 5]; // Remove elements at index 1 and 2 const a2 = [...a1.slice(0, 1), ...a1.slice(3)]; console.log(a2); console.log(a1); Output[1, 4, 5][1, 2, 3, 4, 5]4. Using map() Method with ConditionalsFor more complex tasks, you can use map() to go through each item in an array and make changes. You can also set conditions to skip over items that meet certain criteria, so only the ones you want are included. JavaScript const a1 = [1, 2, 3, 4, 5]; // "Splice" out elements at specific indices const a2 = a1.map((el, index) => (index === 1 || index === 2 ? null : el)).filter(el => el !== null); console.log(a2); console.log(a1); Output[1, 4, 5][1, 2, 3, 4, 5]Importance of Non-Mutating Array SplicingNon-mutating splicing is essential forFunctional Programming: Promotes immutability, reducing side effects.Data Integrity: Ensures original data remains unchanged for reusability.Concurrent Usage: Prevents unintended modifications when sharing data. Comment More infoAdvertise with us Next Article How to Splice an Array Without Mutating the Original Array? D devi_johns Follow Improve Article Tags : JavaScript Web Technologies javascript-array JavaScript-DSA JavaScript-Methods JavaScript-Questions +2 More Similar Reads How To Sort An Array Without Mutating The Original Array In JavaScript? Sorting arrays is a common task in JavaScript, especially when you need to present data in a specific order. However, maintaining the original array is often necessary especially if the original data will be required later in your code. In this article we'll see how we can sort an array in JavaScrip 2 min read How to Update an Array After Splice in Svelte? When you use splice() to modify an array in Svelte, such as adding or removing items, simply calling the method won't trigger a UI update because Svelte relies on reactivity, which requires the array to be reassigned. This article explores two ways to ensure the array updates correctly after a splic 4 min read What is the difference between Array.slice() and Array.splice() in JavaScript ? In JavaScript, slice() and splice() are array methods with distinct purposes. `slice()` creates a new array containing selected elements from the original, while `splice()` modifies the original array by adding, removing, or replacing elements. slice():The slice() method in JavaScript extracts a sec 3 min read How to merge the first index of an array with the first index of second array? The task is to merge the first index of an array with the first index of another array. Suppose, an array is array1 = {a, b, c} and another array is array2 = {c, d, e} if we perform the task on these arrays then the output will be result array { [0]=> array(2) { [0]=> string(1) "a" [1]=> st 3 min read How to Slice an Array in PHP? In PHP, slicing an array means taking a subset of the array and extracting it according to designated indices. When you need to extract a subset of elements from an array without changing the original array, this operation comes in handy. PHP comes with a built-in function called array_slice() to he 2 min read Like