当前位置:网站首页>Split(), split(), slice(), can't you tell?
Split(), split(), slice(), can't you tell?
2022-07-01 03:24:00 【sakuraxiaoyu】
split(),splice(),slice() Stupid is not clear ?
In actual development , Sometimes this happens when you don't pay attention. These functions are mixed up , Let's talk about the properties and usage of these functions in turn .
1. split(separator, limit)
Method : Divide a string into an array of strings , The meanings of the two parameters of this function are as follows :
separator: Optional . String or regular expression , Split from where specified by this parameter string Object.
limit: Optional . This parameter specifies the maximum length of the returned array . If this parameter is set , No more substrings will be returned than the array specified by this parameter . without Set this parameter , The entire string will be split , Regardless of its length .
Be careful :split() Method does not change the original string
var str="How are you doing today?";
var n=str.split(" ");
// n Output the value of an array
How,are,you,doing,today?
2. splice(index, howmany, itemX)
Method :splice() Method to add or remove elements from an array .
| Parameters | describe |
|---|---|
| index | It's necessary . Specify where to add / Remove elements . The parameters are start insertion and ( or ) The subscript of the deleted array element , It has to be numbers . |
| howmany | Optional . Specify how many elements should be removed . It has to be numbers , But it can be “0”. If this parameter is not specified , Delete from index All elements from the beginning to the end of the original array . |
| item1, …, itemX | Optional . New elements to add to the array |
Be careful : This method will change the original array !!
Notice the following code :
function name() {
const [lessonHistoryList, setLessonHistoryList] = useState([]);
// I call the interface data here for storage
const _getLessonHistory = useCallback(async () => {
const res = await getLessonHistory();
const lessonList = res.result || [];
console.log(lessonList)
setLessonHistoryList(lessonHistoryList);
}, []);
}
function renderLessonContent () {
...
{
renderHistoryItem(lessonHistoryList.instanceList)
}
...
}
/* We can see that the data initially obtained from the interface is finally transferred to the following function through the formal parameters of each function If the length of the data is greater than 3 If so, you can intercept it , I used it slice() Method . I already know slice() Method will change the original array , So here comes the question , At this time, I print in the interface res value , Will the data change ? The answer is that it will become . Because the original array is changed , So no matter how the formal parameters of your function pass values , The pointer points to the same memory address , What you seem to operate is renderHistoryItem() Value in function , In fact, it goes back layer by layer ,_getLessonHistory() Inside res It has changed , Because this is actually the value in the same memory address . */
function renderHistoryItem(itemList) {
let showData;
itemList && itemList.length > 3
? (showData = itemList.slice(0, 3))
: (showData = itemList);
}
usage :
Remove the third element of the array , And add a new element in the third position of the array :
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.splice(2,1,"Lemon","Kiwi");
fruits Output results :
Banana,Orange,Lemon,Kiwi,Mango
3. slice(start, end)
Method :slice() Method to return selected elements from an existing array .slice() Method to extract a part of a string , And return the extracted part with a new string .
| Parameters | describe |
|---|---|
| start | Optional . Specify where to start selecting . If the parameter is negative , It means to extract from the penultimate element of the original array ,slice(-2) It means to extract the penultimate element from the original array to the last element ( Contains the last element ). |
| end | Optional . Specify where to end the selection . This parameter is the array subscript at the end of the array fragment . If the parameter is not specified , So the sharded array contains from start All elements to the end of the array . If the parameter is negative , It represents the end of the extraction of the penultimate element in the original array . slice(-2,-1) Indicates that the penultimate element in the original array is extracted to the last element ( Does not contain the last element , That is, only the penultimate element ). |
var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
var citrus = fruits.slice(1,3);
// Output
Orange,Lemon
Be careful :slice() Method does not change the original array .
边栏推荐
- How to use hybrid format to output ISO files? isohybrid:command not found
- # 使用 KubeKey 搭建 Kubernetes/KubeSphere 环境的'心路(累)历程'
- Depth first traversal of C implementation Diagram -- non recursive code
- An article explaining the publisher subscriber model and the observer model
- [applet project development -- Jingdong Mall] classified navigation area of uni app
- Leetcode 1818 absolute value, sorting, dichotomy, maximum value
- 【Qt】添加第三方库的知识补充
- Golang多图生成gif
- Basic concept and classification of sorting
- CX5120控制汇川IS620N伺服报错E15解决方案
猜你喜欢

彻底解决Lost connection to MySQL server at ‘reading initial communication packet

How do spark tasks of 10W workers run? (Distributed Computing)

Hal library setting STM32 interrupt

Ctfshow blasting WP

Chapter 03_ User and authority management

Redis tutorial

POI导出excel,按照父子节点进行分级显示
![[applet project development -- JD mall] uni app commodity classification page (Part 2)](/img/f3/752f41f5b5cc16c8a71498ea9cabb5.png)
[applet project development -- JD mall] uni app commodity classification page (Part 2)

VMware vSphere 6.7 virtualization cloud management 12. Vcsa6.7 update vCenter server license

MySQL index --01--- design principle of index
随机推荐
xxl-job使用指南
[us match preparation] complete introduction to word editing formula
Mysql知识点
Redis 教程
Introduction to EtherCAT
最好用的信任关系自动化脚本(shell)
Go tool cli for command line implementation
8 pits of redis distributed lock
HTB-Lame
Hal library setting STM32 interrupt
If a parent class defines a parameterless constructor, is it necessary to call super ()?
XXL job User Guide
数据交换 JSON
最新接口自动化面试题
Feign remote call and getaway gateway
Huawei operator level router configuration example | BGP VPLS configuration example
[small program project development -- Jingdong Mall] the home page commodity floor of uni app
第03章_用戶與權限管理
调试定位导航遇到的问题总结
如何校验两个文件内容是否相同