JavaScript : How to Copy and Return Part of a JavaScript Array : Array Splice

Java Script @ Freshers.in

JavaScript, arrays are powerful data structures that allow developers to store and manipulate collections of elements easily. One commonly used array method is splice(), which not only removes elements but also has the unique capability of copying part of an array to the same array itself and returning the extracted elements as a new array. In this tutorial, we will explore how to effectively utilize the splice() method to copy and return specific portions of a JavaScript array. Through practical examples and clear explanations, you will learn how to master this technique and apply it proficiently in your projects.

Understanding the splice() Method

The splice() method is a versatile tool in JavaScript arrays that allows you to add, remove, and replace elements within an array. The splice() method in JavaScript arrays provides a powerful and efficient way to copy and return a specific section of an array. By mastering this technique, you can easily manipulate arrays, extract elements, and even insert new ones, all in one go. Remember to be cautious when using splice() on large arrays, as it can potentially impact performance due to the need to shift elements. However, for smaller arrays or when performance is not a critical concern, splice() can be an invaluable tool in your programming arsenal.

array.splice(start, deleteCount, item1, item2, ...);

1. start (required): The index at which to start changing the array.
2. deleteCount (optional): The number of elements to remove from the array.
3. item1, item2, … (optional): The elements to add to the array at the specified start index.

Copying and Returning Part of an Array

The magic of splice() lies in its ability to copy and return a section of the array. To achieve this, we set the deleteCount parameter to 0, which ensures that no elements are removed from the original array. Instead, the elements within the specified range are extracted and returned as a new array.

Example 1: Copying Elements from the Middle

Let’s say we have an array of “freshers_in” programming languages, and we want to extract the second and third elements:

const programmingLanguages = ["freshers_in_JS", "freshers_in_Python", "freshers_in_Ruby", "freshers_in_C"];
const extractedLanguages = programmingLanguages.splice(1, 2);
console.log(extractedLanguages);
// Output: ["freshers_in_Python", "freshers_in_Ruby"]
console.log(programmingLanguages);
// Output: ["freshers_in_JS", "freshers_in_C"]

Example 2: Copying Elements from the End

Now, let’s extract the last two elements from the array:

const extractedLanguages = programmingLanguages.splice(-2);
console.log(extractedLanguages);
// Output: ["freshers_in_Python", "freshers_in_Ruby"]
console.log(programmingLanguages);
// Output: ["freshers_in_JS", "freshers_in_C"]
const extractedLanguages = programmingLanguages.splice(-2);
console.log(extractedLanguages);
// Output: ["freshers_in_Python", "freshers_in_Ruby"]
console.log(programmingLanguages);
// Output: ["freshers_in_JS", "freshers_in_C"]

Example 3: Copying Elements and Inserting New Ones

You can also copy elements and insert new ones at the same time using splice(). Let’s replace the second and third elements with new languages:

const extractedLanguages = programmingLanguages.splice(1, 2, "freshers_in_TypeScript", "freshers_in_Go");
console.log(extractedLanguages);
// Output: ["freshers_in_Python", "freshers_in_Ruby"]
console.log(programmingLanguages);
// Output: ["freshers_in_JS", "freshers_in_TypeScript", "freshers_in_Go", "freshers_in_C"]

Get more articles on Java Script

Author: user

Leave a Reply