当前位置:网站首页>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
边栏推荐
- Introduction to the runner of mmcv
- Design specification for smart speakers v1.0
- 启动jar包报错UnsupportedClassVersionError,如何修复
- Deep understanding of continuation principle
- Opencv learning notes -day2 (implemented by the color space conversion function cvtcolar(), and imwrite image saving function imwrite())
- Esp32 things (x): other functions
- Abstract factory pattern
- Flink 数据偶尔数据积压导致checkpoint失败
- Esp32 things (3): overview of the overall system design
- C#訪問SQL Server數據庫兩種方式的比較(SqlDataReader vs SqlDataAdapter)
猜你喜欢
![[shutter] solve failed assertion: line 5142 POS 12: '_ debugLocked‘: is not true.](/img/77/eb66ec83b34c251e732d495fbaa951.jpg)
[shutter] solve failed assertion: line 5142 POS 12: '_ debugLocked‘: is not true.

100 lines of code and a voice conversation assistant

Self made GIF dynamic graph -gifcam

Dart asynchronous task

Find the number that appears only once in the array

Flink Exception -- No ExecutorFactory found to execute the application

Talk about how the kotlin process started?

Deeply understand the working principle of kotlin collaboration suspend (beginners can also understand it)

El input limit can only input numbers

Deep understanding of kotlin collaboration context coroutinecontext
随机推荐
C#访问MongoDB并执行CRUD操作
将线程绑定在某个具体的CPU逻辑内核上运行
Opencv learning notes-day9 opencv's own color table operation (colormap coloraptypes enumeration data types and applycolormap() pseudo color function)
Advanced technology management -- how managers design and build echelons
Rew acoustic test (I): microphone calibration
Esp32 things (II): sharpening the knife without mistaking firewood - make preparations before project development
How to format an UTC date to use the Z (Zulu) zone designator in php?
Bottomsheetbehavior principle of realizing the home page effect of Gaode map
Maxiouassigner of mmdet line by line interpretation
Understanding society at the age of 14 - reading notes on "happiness at work"
Flink sql -- No factory implements ‘org.apache.flink.table.delegation.ExecutorFactory‘.
File upload component on success event, add custom parameters
Rew acoustic test (IV): test principle of rew
CUDA realizes L2 European distance
[untitled]
Interviewer: do you understand the principle of recyclerview layout animation?
Flink Exception -- No ExecutorFactory found to execute the application
Application of hongruan face recognition
[data analysis and display]
Flink Sql -- toAppendStream doesn‘t support consuming update and delete changes which