当前位置:网站首页>Common methods of array
Common methods of array
2022-07-24 05:54:00 【DniNgL】
Array methods
1.concat() Merge array , Do not change the original array
let arr = [1, 2, 3];
let arr1 = ["a", "b", "c"];
let arr2 = arr.concat(arr1)
console.log(arr2);// [1, 2, 3, "a", "b", "c"]
2.push() Add the contents to the end of the array , And return the modified length
let arr = [1, 2, 3];
console.log(arr.push(6)); // Length of new array 4
3.pop() Remove the last entry in the array , Return the removed value , Reduce the length
let arr = [1, 2, 3];
console.log(arr.pop());// Length of new array after removal 4
5.unshift() Add parameters to the beginning of the original array , And returns the length of the array
let arr = [1, 2, 3];
console.log(arr.push(6)); // Length of new array 4
5.shift() Delete the first item of the original array , And returns the value of the deleted element ; Returns if the array is empty undefined
let arr = [1, 2, 3];
console.log(arr.shift(2)); //1 Subscript of deleted value
6.sort() Sort the items in the array from small to large
let arr = [3,2,7,4,6];
console.log(arr.sort());// [2, 4, 6, 7]
7.reverse() Reverse the order of array items
let arr = [3,2,7,4,6];
console.log(arr.reverse());// [6, 4, 7, 2, 3]
8.slice() Remove elements Two parameters of the method , The first parameter deletes the starting subscript , The second is the ending subscript, but does not include the ending subscript , If the second parameter is none , Then delete the subscript corresponding to the first parameter to the end of the array , in addition , The other parameters can be negative
let arr = [3,2,7,4,6];
console.log(arr.slice(1,3));//[2, 7]
console.log(arr.slice(-3,-1));//[ 7,4]
9.splice() Have deleted , Replace , The function of insertion ; It can be transmitted 3 Parameters , The first starting subscript , the second Delete / Replace Number of elements of , Third Replace / Insert The data of When the second parameter is 0 Time is the function of inserting
let arr = [3, 2, 7, 4, 6];
let arr1 = arr.splice(1, 2, 112, 33, 444)
console.log(arr1); //[2, 7]
console.log(arr); //[3, 112, 33, 444, 4, 6]
10.indexOf() Take two parameters , The first is the item to be found , The second is the subscript of the starting position , If you find the first one , Stop looking , That is, it will not find subsequent elements , Return value when the index of the returned element is found , Return when you can't find it -1
let arr = [3, 2, 7, 4, 6];
console.log(arr.indexOf(2,3));//-1
console.log(arr.indexOf(7,1));//2
11.lastIndexOf() Receive two parameters : The items to find and ( Optional ) Index indicating the location of the search start point , Start at the end of the array and look forward
let arr = [3, 2, 7, 4, 6];
console.log(arr.lastIndexOf(2,1));//1
12.join(“ Put quotes ”) Is to convert an array into a string , Connect with the symbols in brackets , If you don't write, the default is comma ( ,)
let arr = [3, 2, 7, 4, 6];
console.log(arr.join());//3,2,7,4,6
console.log(arr.join("-"));//3-2-7-4-6
Array higher order functions
13.forEach( Traverse )
let arr = [3, 2, 7, 4, 6];
arr.forEach((item, index) => {
console.log( ` Elements :${
item} Subscript ${
index}`);
});
The operation results are as follows 
14.filter( Filter , Return a new array )
let arr = [3, 2, 7, 4, 6];
console.log(arr.filter((item) => {
return item > 3
})); //[7, 4, 6]
15.map( mapping , Return a new array ) Do not change the original array
let arr = [3, 2, 7, 4, 6];
console.log(arr.map((item) => {
return item * 10
})); //[30, 20, 70, 40, 60]
16.some( Determine whether there are qualified elements , Returns a Boolean value ) Return as long as one of the conditions is met ture Return only when you are not satisfied with one false
let arr = [3, 2, 7, 4, 6];
console.log(arr.some((item) => {
return item > 3
}));//true
17.every( Determine whether all elements meet the conditions , Returns a Boolean value ) As long as one of the conditions is not met, it returns false
let arr = [3, 2, 7, 4, 6];
console.log(arr.every((item) => {
return item > 3
}));//false
18.reduce( Add up )
let arr = [3, 2, 7, 4, 6];
console.log(arr.reduce((a, b) => {
return a + b
})); //22
边栏推荐
- ThreadLocal stores the current login user information
- Numpy cheatsheet
- Likeshop100% open source encryption free B2B2C multi merchant mall system
- 删除分类网络预训练权重的的head部分的权重以及修改权重名称
- Help transform traditional games into gamefi, and web3games promote a new direction of game development
- ‘Results do not correspond to current coco set‘
- 多商户商城系统功能拆解08讲-平台端商品分类
- Mysqldump export Chinese garbled code
- "Statistical learning methods (2nd Edition)" Li Hang Chapter 16 principal component analysis PCA mind map notes and after-school exercise answers (detailed steps) PCA matrix singular value Chapter 16
- 第四章 决策树总结
猜你喜欢

《统计学习方法(第2版)》李航 第14章 聚类方法 思维导图笔记 及 课后习题答案(步骤详细) k-均值 层次聚类 第十四章

es6常用特性

第四章 决策树总结

《统计学习方法(第2版)》李航 第17章 潜在语义分析 LSA LSI 思维导图笔记 及 课后习题答案(步骤详细)第十七章

PDF文本合并

如何在网页上下载视频

Chapter III summary of linear model

"Statistical learning methods (2nd Edition)" Li Hang Chapter 16 principal component analysis PCA mind map notes and after-school exercise answers (detailed steps) PCA matrix singular value Chapter 16

删除分类网络预训练权重的的head部分的权重以及修改权重名称

Could not load library cudnn_ cnn_ infer64_ 8.dll. Error code 126Please make sure cudnn_ cnn_ infer64_ eight
随机推荐
likeshop | 单商户商城系统代码开源无加密-PHP
【USB Host】STM32H7 CubeMX移植带FreeRTOS的USB Host读取U盘,USBH_Process_OS卡死问题,有个值为0xA5A5A5A5
找数组中出现次数最多的数
统计信号处理小作业——瑞利分布噪声中确定性直流信号的检测
MySQL batch insert demo
GCC 中__attribute__((constructor)和__attribute__(((destructor))的注意事项。
[activiti] Introduction to activiti
OSError: [WinError 127] 找不到指定的程序。Error loading “caffe2_detectron_ops.dll“ or one of its dependencies
电商系统PC商城模块介绍
‘Results do not correspond to current coco set‘
STM32标准外设库(标准库)官网下载方法,附带2021最新标准固件库下载链接
Multi merchant mall system function disassembly lecture 08 - platform end commodity classification
Likeshop100% open source encryption free B2B2C multi merchant mall system
Machine learning (Zhou Zhihua) Chapter 4 notes on learning experience of decision tree
多商户商城系统功能拆解06讲-平台端商家入驻协议
Points for attention in adding spp module to the network
多商户商城系统功能拆解08讲-平台端商品分类
测试数据增强后标签和数据集是否对应
《机器学习》(周志华)第2章 模型选择与评估 笔记 学习心得
The SaaS mall system of likeshop single merchant is built, and the code is open source without encryption.