27 C
Jaipur
Tuesday, December 1, 2020

Applying JavaScript’s Splice Function – Linux Hint

Must read

How To Launch Fish Shell In Private Mode In Linux

Fish shell ships with many cool features by default. One such feature is Private Mode. In this brief guide, we will see how to...

Toyota Urban Cruiser Vs 2020 Maruti Vitara Brezza: Which One Is Better?

Interestingly, the 2020 Maruti Suzuki Vitara Brezza and Toyota Urban Cruiser are two same cars with different company badges. They have the same engine,...

Uninstall Apps On Mac: 3 Easy Ways To Clean The Clutter

If you’ve accumulated a ton of apps and don’t need them cluttering your storage anymore, here are 3 easy ways to uninstall apps on...

JavaScript is a lightweight programming language, and as with any programming language, when developing JavaScript programs, we often need to work with arrays to store data. In this article, we will introduce JavaScript’s built-in splice function and discuss how we can use it to manipulate an array. As data are generated, the structures used for storage must be updated. For this reason, a programmer must often add elements to or remove elements from an array.

The splice function is used to add elements to or remove elements from an array at a given index, and it returns the elements removed from the array. The syntax for the splice function is as follows:

 array.splice(index, removeCount, items…)

Here, index is the position at which we want to add or remove elements, removeCount, which is an optional argument, is the number of elements that we want to remove, and items, which is also optional, contains the elements we want to add.

Now, we will go over a few examples to show how the splice function is implemented.

First, suppose we have an array that consists of five elements.

 let arr = [10,20,30,40,50]

To remove the elements 20 and 30 (at position 1 and position 2 in the array, respectively) from the array, we simply call the splice function and tell it to start from the first index and remove 2 elements.


The values 20 and 30 are returned as the output. Next, we can look at the original array with the following command:


The two elements returned in the output are no longer in the array.

Next, we will add elements to the array using the splice function. Because we will not remove elements from the array, we can provide a value of zero for removeCount and then provide the elements we want to add.

 arr.splice(2, 0, 30, 35);


The above command returns an empty array because no elements were removed. However, if we look at the original array, we can see that it has been updated.

The values 30 and 35 were successfully added at the second index.

Finally, if we want to remove elements and add elements, we can provide values for both removeCount and items.

 arr.splice(1, 2, 15, 20, 25);

The above command has returned the two elements that were removed, and if we print the original array to the console, we can see that 20 and 30 are no longer in the array and that 15, 20 and 25 have been added.

Conclusion

In this article, we discussed several ways to use the splice function to update arrays. We hope you found this article useful and continue to learn JavaScript with linuxhint.com.

Source link

- Advertisement -

More articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest article

How To Launch Fish Shell In Private Mode In Linux

Fish shell ships with many cool features by default. One such feature is Private Mode. In this brief guide, we will see how to...

Toyota Urban Cruiser Vs 2020 Maruti Vitara Brezza: Which One Is Better?

Interestingly, the 2020 Maruti Suzuki Vitara Brezza and Toyota Urban Cruiser are two same cars with different company badges. They have the same engine,...

Uninstall Apps On Mac: 3 Easy Ways To Clean The Clutter

If you’ve accumulated a ton of apps and don’t need them cluttering your storage anymore, here are 3 easy ways to uninstall apps on...