当前位置:网站首页>Splice and slice functions of JS
Splice and slice functions of JS
2022-06-30 09:08:00 【Ancient dust left Taidao】
Have a look today JS Of Splice and slice, I was always confused before
Splice
it is to be noted that splice Method, you can modify the !!!
Splice Methods can be Replace , modify , Delete Elements in an array , This method I understand as receiving 3 Parameters , See table for details .
| Variable name | start | deleteCount | item1,item2,item3,…more |
|---|---|---|---|
| describe | Indexes , It can be understood as the array position to be manipulated ( This place can pass negative numbers , Pass negative numbers , You count backwards -1 Is the last element , Because the first element index is 0 Well ) | The number of elements to be deleted from the index ( This place can pass negative numbers , Pass negative numbers , You count backwards -1 Is the last element , Because the first element index is 0 Well ) But write negative numbers and write 0 It's all the same , Don't delete elements , It doesn't make sense | Number of elements to add |
| Whether it is necessary to transmit | no ( Not transmitting is equivalent to starting from the index position , Delete all the following elements ) | no | no |
Example
Example 1
const arr = [1, 2, 3]
const orderArr = arr.splice(1)
console.log(arr) // [1]
console.log(orderArr) // [2, 3]
so splice The return value of the method is the element you deleted , The array itself has also been modified
Example 2
const arr = [1, 2, 3]
// The index is 1 Position of 4, Do not delete the elements in the array
const orderArr = arr.splice(1, 0, 4)
console.log(arr) // [1, 4, 2, 3]
console.log(orderArr) // []
Example 3
const arr = [1, 2, 3]
// The index is 1 Position of 4,5, Do not delete the elements in the array
const orderArr = arr.splice(1, 0, 4, 5)
// But I think it's more elegant
// arr.splice(1, 0, ...[4,5])
console.log(arr) // [1, 4, 5, 2, 3]
console.log(orderArr) // []
Example 4
const arr = [1, 2, 3]
// First delete the index as 1 The elements of , And then 4 Insert into index as 1 The place of
const orderArr = arr.splice(1, 1, 4)
console.log(arr) // [1, 4, 3]
console.log(orderArr) // [2]
Example 5
const arr = [1, 2, 3]
// Delete index as -2 The elements of
const orderArr = arr.splice(-2, 1)
console.log(arr) // [1, 3]
console.log(orderArr) [2]
Example 6
const arr = [1, 2, 3]
// Delete index as -2 To all subsequent elements
const orderArr = arr.splice(-2)
console.log(arr) // [1]
console.log(orderArr) // [2, 3]
Slice
slice Method does not modify the original array !!!
slice Method reception 2 Parameters
| Variable name | begin | end |
|---|---|---|
| describe | The starting position to intercept | The end position to intercept ( Does not include end position ) |
| Whether it is necessary to transmit | no ( The default value is 0, You can also pass negative numbers ) | no ( Do not pass the default to the end of the array , You can also pass negative numbers ) |
Example
const arr = [1, 2, 3]
const orderArr = arr.slice(1, 3)
console.log(arr) // [1, 2, 3]
console.log(orderArr) // [2, 3]
tip
splice Method Vue It can be monitored , That is, you adjust the array splice Method , The change of array can affect the change of view
Blog
Welcome to my blog www.smartxy.cc
边栏推荐
- CUDA implements matrix replication
- 挖财开户安全吗?怎么有人说不靠谱。
- Esp32 things (I): Preface
- [untitled]
- [data analysis and display]
- Rew acoustic test (II): offline test
- Comparaison de deux façons d'accéder à la base de données SQL Server (sqldatareader vs sqldataadapter)
- [untitled]
- Pytorch BERT
- Axure make menu bar effect
猜你喜欢

Rew acoustic test (I): microphone calibration

Flink Exception -- No ExecutorFactory found to execute the application

Talk about how the kotlin collaboration process establishes structured concurrency
![[data analysis and display]](/img/86/19260ee664769c98588d8b0783ef28.jpg)
[data analysis and display]
![[wechat applet] realize applet pull-down refresh and pull-up loading](/img/23/2668a3a36fd46f63732c753fd6f237.jpg)
[wechat applet] realize applet pull-down refresh and pull-up loading

Esp32 things (3): overview of the overall system design

Implementing custom drawer component in quick application

Occasionally, Flink data is overstocked, resulting in checkpoint failure

Interpretation of orientedrcnn papers

Detailed explanation of pytoch's scatter function
随机推荐
挖财开户安全吗?怎么有人说不靠谱。
Advanced technology management -- how managers design and build echelons
Understanding society at the age of 14 - reading notes on "happiness at work"
Explanation on the use of password profiteering cracking tool Hydra
[cmake] make command cannot be executed normally
Implementing custom drawer component in quick application
Bind threads to run on a specific CPU logical kernel
So the toolbar can still be used like this? The toolbar uses the most complete parsing. Netizen: finally, you don't have to always customize the title bar!
Why must redis exist in distributed systems?
[untitled]
Viteproject require Syntax Compatibility Problem Solving require is not defined
C#訪問SQL Server數據庫兩種方式的比較(SqlDataReader vs SqlDataAdapter)
vite項目require語法兼容問題解决require is not defined
Opencv learning notes -day3 (mat object and creation related operations mat:: clone(), mat:: copyto(), mat:: zeros(), mat:: ones(), scalar()...)
mysql基础入门 动力节点[老杜]课堂作业
Vite project require syntax compatibility problem solving require is not defined
C#访问MongoDB并执行CRUD操作
Codeworks 5 questions per day (1700 for each) - the third day
证券开户的优惠怎样才能得到?在线开户安全?
[data analysis and display]