当前位置:网站首页>Slice() and slice() methods of arrays in JS
Slice() and slice() methods of arrays in JS
2022-06-25 12:38:00 【Fall in love with*】
slice() You can extract a specified element from an array , Specify how the element is indexed
This method does not change its own array , Instead, the extracted elements are encapsulated into a new array and returned
It should be noted that :
Intercept the starting index , Include start index
Intercepted index , Does not include end index
arr.slice(3,5) The inclusion index is 3 Value But does not contain an index of 5 Value
arr.slice(a,b) The second parameter can be omitted , All elements from the beginning of the index will be intercepted
Note that an index can assign a value to Arrays can be -1 Start From back to front -1 The last one -2 second to lastsplice() Delete the element and add a new element to the array
Use splice() Will affect the original array , And return the deleted element as the return value
It should be noted that :
splice(a,b,c) a Is the index of the start position b Is the number of deletions c And later you can add any elementsplice() Method can not only delete elements You can also add new elements based on the deleted elements
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script type="text/javascript">
/*
slice() You can extract a specified element from an array , Specify how the element is indexed
This method does not change its own array , Instead, the extracted elements are encapsulated into a new array and returned
It should be noted that :
Intercept the starting index , Include start index
Intercepted index , Does not include end index
arr.slice(3,5) The inclusion index is 3 Value But does not contain an index of 5 Value
arr.slice(a,b) The second parameter can be omitted , All elements from the beginning of the index will be intercepted
Note that an index can assign a value to Arrays can be -1 Start From back to front -1 The last one -2 second to last
*/
var arr = [90,23,98,56,53,23,12,6];
var result = arr.slice(3,7);
console.log(result);
// Will be output {"0":56,"1":53,"2":23,"3":12,"length":4}
var result1 = arr.slice(2);
console.log(result1);
// Will be output {"0":98,"1":56,"2":53,"3":23,"4":12,"5":6,"length":6}
var result2 = arr.slice(1,-3);
console.log(result2);
// Will be output {"0":23,"1":98,"2":56,"3":53,"length":4}
/*
splice() Delete the element and add a new element to the array
Use splice() Will affect the original array , And return the deleted element as the return value
It should be noted that :
splice(a,b,c) a Is the index of the start position b Is the number of deletions c And later you can add any element
*/
arr.splice(0,2);
console.log(arr);
// Will be output {"0":53,"1":23,"2":12,"3":6,"length":4}
var result3 = arr.splice(0,2);
console.log(result3);
// Will be output {"0":98,"1":56,"length":2}
// Will be output
//splice() Method can not only delete elements You can also add new elements based on the deleted elements
arr.splice(0,3,"100");
console.log(arr);
// Will be output {"0":"100","1":56,"2":53,"3":23,"4":12,"5":6,"length":6}
</script>
</head>
<body>
</body>
</html>
边栏推荐
- R language uses the scale function to scale the input data of neural network to the minimum and maximum, scale the data to between 0 and 1, and divide the data set into training set and test set
- R language uses the multinom function of NNET package to build an unordered multi classification logistic regression model, and uses the summary function to obtain the summary statistical information
- High imitation blue playing network disk file sharing to make money network disk PHP system source code
- R language dplyr package summary_ The at function calculates the count number, mean and median of multiple data columns (specified by vectors) in the dataframe data, and specifies na RM parameter, spe
- Network | indicators and test methods to measure the quality of the network
- Renrenyue -- renrenyue system development source code sharing
- Ramda rejects objects with null and empty object values in the data
- Total number of MySQL statistics, used and unused
- 冷启动的最优解决方案
- PHP takes the difference set of two arrays
猜你喜欢

Go novice exploration road 1
![按权重随机选择[前缀和+二分+随机target]](/img/84/7f930f55f8006a4bf6e23ef05676ac.png)
按权重随机选择[前缀和+二分+随机target]

Idea2017 how to set not to automatically open a project at startup

Zhangxiaobai's way of penetration (V) -- detailed explanation of upload vulnerability and parsing vulnerability

GPS receiver design (1)

An article clearly explains MySQL's clustering / Federation / coverage index, back to table, and index push down

Matlab simulation of m-sequence

Guess Tongyuan B

Explain AHP in human language (very detailed principle + simple tool implementation)

The server reported an error 503 service unavailable:the system returned: (71) protocol error
随机推荐
Some fields are ignored in tp6 query
Mysql database logs binlog save aging (expire\u logs\u days)
Wechat forbids sharing
(6) Pyqt5--- > window jump (registration login function)
Zhangxiaobai's way of penetration (III) -- detailed explanation of SQL injection vulnerability principle (SQL Server)
Ubuntu uninstalling PHP
PHP appends the same elements to a two-dimensional array
Laravel multi project mutual access
Slice and slice methods of arrays in JS
Laravel echart statistical chart line chart
Arm V7 continuous load / store
K8s, docker compose install MySQL 8.0.18
The difference between this and super and their respective functions
JS array length is defined
Foreach method of array in JS
2022 meisai topic C idea sharing + translation
Jeecgboot startup popup configuration is still incorrect
Huikan source code -- Huikan app system secondary development source code sharing
Hook technology
Guess Tongyuan B