当前位置:网站首页>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
边栏推荐
- Get to know handler again
- Application of hongruan face recognition
- Deep understanding of continuation principle
- Duplicate entry '2' for key 'primary appears in JPA‘
- Occasionally, Flink data is overstocked, resulting in checkpoint failure
- Agp7.0|kts makes a reinforced plug-in
- Dart basic notes
- Invalid update: invalid number of sections. The number of sections contained in the table view after
- [cmake] make command cannot be executed normally
- Opencv learning notes -day13 pixel value statistics calculation of maximum and minimum values, average values and standard deviations (use of minmaxloc() and meanstddev() functions)
猜你喜欢

Rew acoustic test (II): offline test

Talk about the job experience of kotlin cooperation process
![[untitled]](/img/45/368c41a74bf0738369c58c5963fca0.jpg)
[untitled]

Advanced technology management -- how managers design and build echelons
![[data analysis and display]](/img/86/19260ee664769c98588d8b0783ef28.jpg)
[data analysis and display]

使用华为性能管理服务,按需配置采样率

Dart asynchronous task

Implementing custom drawer component in quick application

Do you want the dialog box that pops up from the click?

Abstract factory pattern
随机推荐
Talk about the kotlin cooperation process and the difference between job and supervisorjob
JVM调优相关命令以及解释
Talk about how the kotlin process started?
Esp32 things (VIII): music playing function of function development
List set export excel table
Vite project require syntax compatibility problem solving require is not defined
Introduction to MySQL basics day4 power node [Lao Du] class notes
14岁懂社会-《关于“工作的幸福”这件事儿》读书笔记
Opencv learning notes -day10 logical operation of image pixels (usage of rectangle function and rect function and bit related operation in openCV)
Detailed explanation of pipline of mmdetection
Flutter theme (skin) changes
[paid promotion] collection of frequently asked questions, FAQ of recommended list
使用华为性能管理服务,按需配置采样率
Flink Sql -- toAppendStream doesn‘t support consuming update and delete changes which
Esp32 things (I): Preface
Detectron2 source code reading 3-- encapsulating dataset with mapper
Detailed explanation of pytoch's scatter function
vim 从嫌弃到依赖(21)——跨文件搜索
C#訪問SQL Server數據庫兩種方式的比較(SqlDataReader vs SqlDataAdapter)
Flink Sql -- toAppendStream doesn‘t support consuming update and delete changes which