当前位置:网站首页>Common prototype methods of JS array
Common prototype methods of JS array
2022-07-02 06:50:00 【xuefankang】
Array prototype methods mainly include the following :
- join()
- push() and pop()
- shift() and unshift()
- sort()
- reverse()
- concat()
- slice()
- splice()
- indexOf() and lastIndexOf()
- forEach()
- map()
- filter()
- every()
- some()
The basic functions of each method are described in detail below .
1、join()
Group elements of an array into a string , With separator Separator , If omitted, comma is used as separator by default , This method accepts only one parameter : Delimiter
var arr = [1,2,3,4,5];
console.log(arr.join(",")); // 1,2,3,4,5
2、push() and pop()
push(): Can receive any number of parameters , Add them to the end of the array one by one , And return the length of the modified array .
pop(): Remove last entry at end of array , Reduce the length value , Then return the removed item .
var arr = [" Zhang San "," Li Si "];
arr.push(" Wang Wu "," Zhao Liu ");
console.log(arr); // [" Zhang San ", " Li Si ", " Wang Wu ", " Zhao Liu "]
var item = arr.pop();
console.log(item); // Zhao Liu
console.log(arr); // [" Zhang San ", " Li Si ", " Wang Wu "]
3、unshift() and shift()
unshift(): Add parameters to the beginning of the original array , And returns the length of the array .
shift(): Delete the first item of the original array , And returns the value of the deleted element ; Returns if the array is empty undefined .
var arr = [" Zhang San "," Li Si "," Wang Wu "];
arr.unshift(" Zhao Liu ");
console.log(arr); // [" Zhang San ", " Li Si ", " Wang Wu ", " Zhao Liu "]
var item = arr.shift();
console.log(item); // Zhao Liu
console.log(arr); // [" Zhang San ", " Li Si ", " Wang Wu "]
This set of methods is the same as the above push() and pop() The method exactly corresponds to , One is the beginning of the operation array , One is the end of the operation array .
4、sort()
sort(): Array items in ascending order —— That is, the smallest value is at the front , The largest value is at the bottom .
var arr = [2, 45, 11, 36, 29];
console.log(arr.sort()); // [2, 11, 29, 36, 45]
5、reverse()
reverse(): Reverse the order of array items .
var arr = [13, 24, 51, 3];
console.log(arr.reverse()); //[3, 51, 24, 13]
6、concat()
concat() : Add parameters to the original array . This method will first create a copy of the current array , Then add the received parameters to the end of this copy , Finally, return the newly constructed array .
var arr1 = [1, 3, 5, 7];
var arr2 = [2, 4, 6, 8];
console.log(arr1.concat(arr2); //[1, 3, 5, 7, 2, 3, 6 ,8]
7、slice()
slice(): Returns a new array of items from the specified start subscript to the end subscript in the original array .slice() Methods can take one or two arguments , That is, to return the start and end positions of the item . With only one parameter , slice() Method returns all items from the position specified by the parameter to the end of the current array . If you have two parameters , This method returns the item between the start and end positions —— But it doesn't include the end position .
var arr = [1,3,5,7,9,11];
var arrRes1 = arr.slice(1);
var arrRes2 = arr.slice(1,4);
var arrRes3 = arr.slice(1,-2);
var arrRes4 = arr.slice(-4,-1);
console.log(arrRes1); // [3, 5, 7, 9, 11]
console.log(arrRes2); // [3, 5, 7]
console.log(arrRes3); // [3, 5, 7]
console.log(arrRes4); // [5, 7, 9]
8、splice()
splice(): Very powerful array method , It has many uses , Can be deleted 、 Insert and replace .
- Delete : You can delete any number of items , Just specify 2 Parameters : The location of the first item to delete and the number of items to delete .
- Insert : You can insert any number of items... Into a specified location , Just provide 3 Parameters : The starting position 、 0( Number of items to delete ) And the item to insert .
- Replace : You can insert any number of items... Into a specified location , And delete any number of items at the same time , Just specify 3 Parameters : The starting position 、 The number of items to delete and any number of items to insert . You don't have to insert as many items as you delete .
splice() Method always returns an array , The array contains items that were removed from the original array , If nothing is deleted , Then return an empty array .
var arr = [1,3,5,7,9,11];
var arrRes1 = arr.splice(0,2);
console.log(arr); // [5, 7, 9, 11]
console.log(arrRes1); // [1, 3]
var arrRes2 = arr.splice(2,0,4,6);
console.log(arr); // [5, 7, 4, 6, 9, 11]
console.log(arrRes2); // []
var arrRes3 = arr.splice(1,1,2,4);
console.log(arr); // [5, 2, 4, 4, 6, 9, 11]
console.log(arrRes3); // [7]
9、indexOf() and lastIndexOf()
indexOf(): Receive two parameters : The items to find and ( Optional ) Index indicating the location of the search start point . among , From the beginning of the array ( Location 0) Start looking backwards .
lastIndexOf: Receive two parameters : The items to find and ( Optional ) Index indicating the location of the search start point . among , Start at the end of the array and look forward .
var arr = [1,3,5,7,7,5,3,1];
console.log(arr.indexOf(5)); //2
console.log(arr.lastIndexOf(5)); //5
console.log(arr.indexOf(5,2)); //2
console.log(arr.lastIndexOf(5,4)); //2
console.log(arr.indexOf("5")); //-1
Both methods return the position of the item to be found in the array , Or return if you can't find it -1. When comparing the first parameter with each item in the array , Will use congruent operators .
10、forEach()
forEach(): Loop through the array , Run the given function for each item in the array . This method does not return a value . Parameters are function type , By default, there are parameters , The parameters are : Traversing the contents of the array ; The corresponding array index , The array itself .
var arr = [1, 2, 3, 4, 5];
arr.forEach(function(i, index){
console.log(i + '|' + index);
});
// Output is :
// 1|0
// 2|1
// 3|2
// 4|3
// 5|4
11、map()
map(): finger “ mapping ”, Run the given function for each item in the array , Returns an array of the results of each function call .
The following code uses map Method to square each number in the array .
var arr = [1, 2, 3, 4, 5];
var arrRes = arr.map(function(item){
return item*item;
});
console.log(arrRes ); //[1, 4, 9, 16, 25]
12、filter()
filter():“ Filter ” function , Each item in the array runs the given function , Returns an array that meets the filter conditions .
var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var arrRes = arr.filter(function(i, index) {
return index % 3 === 0 || i >= 8;
});
console.log(arrRes ); //[1, 4, 7, 8, 9, 10]
13、every()
every(): Determine whether each item in the array satisfies the condition , Only all terms meet the condition , Will return true.
var arr = [1, 2, 3, 4, 5];
var arrRes1 = arr.every(function(i) {
return i < 10;
});
console.log(arrRes1); // true
var arrRes2 = arr.every(function(i) {
return i < 3;
});
console.log(arrRes2); // false
14、some()
some(): Determine whether there are items in the array that meet the conditions , As long as one of the conditions is met , It will return true.
var arr = [1, 2, 3, 4, 5];
var arrRes1 = arr.some(function(i) {
return i < 3;
});
console.log(arrRes1); // true
var arrRes2 = arr.some(function(i) {
return i < 1;
});
console.log(arrRes2); // false
边栏推荐
- Promise中有resolve和无resolve的代码执行顺序
- apt命令报证书错误 Certificate verification failed: The certificate is NOT trusted
- FE - Eggjs 结合 Typeorm 出现连接不了数据库
- Automation - when Jenkins pipline executes the nodejs command, it prompts node: command not found
- Apt command reports certificate error certificate verification failed: the certificate is not trusted
- Win10: add or delete boot items, and add user-defined boot files to boot items
- Log - 7 - record a major error in missing documents (A4 paper)
- virtualenv和pipenv安装
- After reading useful blogs
- How to debug wechat built-in browser applications (enterprise number, official account, subscription number)
猜你喜欢
Redis -- cache breakdown, penetration, avalanche
Thread hierarchy in CUDA
sqli-labs通关汇总-page3
The use of regular expressions in JS
In depth study of JVM bottom layer (V): class loading mechanism
Win电脑截图黑屏解决办法
Queue (linear structure)
Latex warning: citation "*****" on page y undefined on input line*
The default Google browser cannot open the link (clicking the hyperlink does not respond)
Latex error: the font size command \normalsize is not defined problem solved
随机推荐
QQ email cannot receive the email sent by Jenkins using email extension after construction (timestamp or auth...)
js中map和forEach的用法
[daily question] - Huawei machine test 01
Date time API details
Implement strstr() II
Latex error: the font size command \normalsize is not defined problem solved
uniapp引入本地字体
CUDA and Direct3D consistency
Win10网络图标消失,网络图标变成灰色,打开网络设置闪退等问题解决
Sublime text configuring PHP compilation environment
VSCODE 安装LATEX环境,参数配置,常见问题解决
js删除字符串的最后一位
20201002 vs 2019 qt5.14 developed program packaging
ctf三计
Build learning tensorflow
Win电脑截图黑屏解决办法
FE - Weex 使用简单封装数据加载插件为全局加载方法
FE - 微信小程序 - 蓝牙 BLE 开发调研与使用
(第一百篇BLOG)写于博士二年级结束-20200818
Huawei mindspire open source internship machine test questions