Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the use of JavaScript array splice

Shulou Source: shulou.com Published: 2022-06-03 14:21:21 10月01日 Update

This article is to share with you about the use of JavaScript array splice, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

1. Delete, you can delete any number of items. You only need to specify two parameters. The location of the first item to delete and the number of items to delete.

For example, splice (0Pol 2) will delete two items starting with index 0.

2. Insert, specify the location to insert any number of items.

You only need to provide three parameters: the starting position, 0 (the number of items to delete), and one or more items to insert. For example, splice (2 red 0, "red", "green") will insert the strings "red" and "green" from the current array position 2.

3. Replace, you can insert any number of items into the specified location and delete any number of items at the same time.

You only need to specify three parameters: the starting position, the number of items to delete, and any number of items to insert. The number of items inserted does not have to be equal to the number of items deleted.

Example

Var colors = ["red", "green", "blue"]; var removed = colors.splice (0Magne1); / / delete the first item alert (colors); / / green,bluealert (removed); / / red, the returned array contains only one item removed = colors.splice (1,0, "yellow", "orange"); / / insert two items alert (colors) from position 1; / / green,yellow,orange,bluealert (removed) / / returns an empty array removed = colors.splice (1,1, "red", "purple"); / / inserts two items and deletes an alert (colors); / / green,red,purple,orange,bluealert (removed); / / yellow. The returned array contains more than one item, that is, the use of the JavaScript array splice. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Tags: Location array item quantity parameter just need number of items three two more knowledge article number of items starting practical at the same time multiple characters strings instances Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Apple Shulou Technology MariaDB Xiaomi