


What is the difference between the functions slice, splice and split in js
I feel so confused every time I encounter these three values. It's hard to tell them apart every time. Don't even know how to use that. Finally, let’s summarize today.
slice()
Note: This method does not change the original array. Parameters containing headers do not contain unspecified numbers
arrayObject.slice(start,end);
You can intercept both strings and arrays. What is returned is a new array.
slice() method returns selected elements from an existing array.
Return a new array,. The parameter start is the starting array index of interception, and the end parameter is equal to the position value of the last character you want to retrieve plus 1 (optional) (including header but not ending)
If the second parameter is omitted, it defaults to the intercepted field From the starting position to the ending position
For example:
`//如果不传入参数二,那么将从参数一的索引位置开始截取,一直到数组尾 var a=[1,2,3,4,5,6]; var b=a.slice(0,3); //[1,2,3] var c=a.slice(3); //[4,5,6] //如果两个参数中的任何一个是负数,array.length会和它们相加,试图让它们成为非负数,举例说明: //当只传入一个参数,且是负数时,length会与参数相加,然后再截取 var a=[1,2,3,4,5,6]; var b=a.slice(-1); //[6]
//当只传入一个参数,是负数时,并且参数的绝对值大于数组length时,会截取整个数组 var a=[1,2,3,4,5,6]; var b=a.slice(-6); //[1,2,3,4,5,6] var c=a.slice(-8); //[1,2,3,4,5,6]//当传入两个参数一正一负时,length也会先于负数相加后,再截取 var a=[1,2,3,4,5,6];var b=a.slice(2,-3); //[3]//当传入一个参数,大于length时,将返回一个空数组 var a=[1,2,3,4,5,6];var b=a.slice(6); //[]//截取一个字符串时var a="i am a boy"; var b=a.slice(0,6); //"i am a"`
2 splice()
arrayObject.splice(index,howmany,item1,….. ,itemX);
splice() method adds/removes items to/from the array, and then returns the deleted item.
Note: This method will change the original array.
The first and second parameters are required. And it will differ depending on whether the second parameter is 0.
As follows
The first parameter specifies the position to add/delete items from the starting position, and use negative numbers to specify the position from the end of the array.
The second parameter is the number of items to be deleted. If set to 0, the item will not be deleted
1. Delete - used to delete elements, two parameters, the first parameter (the position of the first item to be deleted), the second parameter (the item to be deleted Number).
var lang = ['php', 'java', 'javascript']; var removed = lang.splice(1,1); //删除 console.log(lang); // php, javascript console.log(removed); //java
2. Insert - Insert any element into the specified position in the array. Three parameters, the first parameter (the real position), the second parameter (0), and the third parameter (the inserted item).
var insert = lang.splice(0, 0, 'asp'); //从第0个位置开始插入 console.log(insert); //空数组 console.log(lang); //asp, php,javascript
3. Replacement - insert any item element into the specified position in the array, and delete any number of items at the same time, three parameters. The first parameter (the starting position), the second parameter (the number of items to delete), and the third parameter (to insert any number of items).
var replace = lang.splice(1,1,"c#","ruby"); //删除一项,插入两项 console.log(lang); //console.log(replace); //返回删除的项
split(); stringObject.split(separator,howmany)
Note: If an empty string ("") is used as a separator, each character in the stringObject will be split. String.split() does the opposite of what Array.join does.
Used to split a string into a string array. An array of strings. The array is created by splitting the string stringObject into substrings at the boundaries specified by separator. The strings in the returned array do not include the separator itself.
However, if separator is a regular expression that contains subexpressions, the returned array includes strings that match those subexpressions (but not text that matches the entire regular expression).
1. If "." is used as a separation, it must be written as follows: String.split("\."), so that it can be separated correctly. String.split(".");
2. If "|" is used as a separator, it must be written as follows: String.split("\|"), so that it can be separated correctly. String.split("|") cannot be used;
3 . If "\" is used as the separation, it must be written as follows: String.split(\), so that it can be separated correctly. String.split("\") cannot be used;
".", "| " and "\" are both escape characters, and "\" must be added;
3. If there are multiple delimiters in a string, you can use "|" as a hyphen, such as: "acount=? and uu =? or n=?", to separate all three, you can use String.split("and|or");
var str="How are you doing today?" document.write(str.split(" ") + "<br />") document.write(str.split("") + "<br />") document.write(str.split(" ",3)) </script>输出: How,are,you,doing,today? H,o,w, ,a,r,e, ,y,o,u, ,d,o,i,n,g, ,t,o,d,a,y,? How,are,you例子 2 在本例中,我们将分割结构更为复杂的字符串: "2:3:4:5".split(":") //将返回["2", "3", "4", "5"] "|a|b|c".split("|") //将返回["", "a", "b", "c", ""] 例子 3 使用下面的代码,可以把句子分割成单词: var words = sentence.split(' ')或者使用正则表达式作为 separator: var words = sentence.split(/\s+/)例子 4 如果您希望把单词分割为字母,或者把字符串分割为字符,可使用下面的代码: "hello".split("") //可返回 ["h", "e", "l", "l", "o"]若只需要返回一部分字符,请使用 howmany 参数: "hello".split("", 3) //可返回 ["h", "e", "l"]
The above is the detailed content of What is the difference between the functions slice, splice and split in js. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

JavaScript is a programming language widely used in web development, while WebSocket is a network protocol used for real-time communication. Combining the powerful functions of the two, we can create an efficient real-time image processing system. This article will introduce how to implement this system using JavaScript and WebSocket, and provide specific code examples. First, we need to clarify the requirements and goals of the real-time image processing system. Suppose we have a camera device that can collect real-time image data
