当前位置:网站首页>JS array common methods
JS array common methods
2022-06-28 22:12:00 【wo4gu5zai】
js Array common methods
1. Array.push()
Add one or more elements... To the end of the array , And returns the new array length . The original array changes .
let arr = ["a","b","c"]
arr.push("d")
console.log(arr) // ["a","b","c","d"]
2. Array.pop()
Delete and return the last element of the array , If the array is empty , Then return to undefined. The original array changes .
let arr = [1,2,3,4]
let last = arr.pop()
console.log(arr,last) // [1,2,3] 4
3. Array.unshift()
Add one or more elements... To the beginning of the array , And returns the new array length . The original array changes .
let arr = [1,2,3]
let length = arr.unshift(0)
console.log(arr,length) // [0,1,2,3] 4
4. Array.shift()
Delete the first item of the array , And returns the value of the first element . If the array is empty , Then return to undefined. The original array changes .
let arr = [1,2,3,4]
let first = arr.shift()
console.log(arr,first) // [2,3,4] 1
5. Array.concat(arr1,arr2…)
Merge two or more arrays , Generate a new array . The original array remains the same .
let arr1 = [1,2,3]
let arr2 = [4,5,6]
let arr = arr1.concat(arr1)
console.log(arr) // [1,2,3,4,5,6]
6. Array.join()
Connect each item of the array with a specified character to form a string . The default connection character is “,” comma .
let arr = [1,2,3]
let str1 = arr.join()
let str2 = arr.join('-')
conosle.log(str1,str2) // 1,2,3 1-2-3
7. Array.reverse()
Reverse array . The original array changes .
let arr = [1,2,3]
let reArr = arr.reverse()
console.log(reArr) // [3,2,1]
8. Array.sort()
Sort array elements . According to the string UniCode Sort code , The original array changes .
arr = [2,1,3]
strArr = [{
name:"zhangsan",age:18},{
name:"lisi",age:17},{
name:"wangwu",age:19}]
let comparator1 = function(a,b){
return a - b
}
arr.sort(comparator1) // [1,2,3]
let comparator2 = function(a,b){
return b - a
}
arr.sort(comparator2) // [3,2,1]
function compare(param){
return function sortAge(a,b){
return a[param] - b[param]
}
}
arr.sort(compare("age"))
9. Array.some()
Judge each item in the array , If none of them meet the requirements, return to false, Otherwise return to true.
let arr = [1,2,3]
console.log(arr.some(e => e > 2)) //true
10. Array.every()
Judge each item in the array , If all match, return true, Otherwise return to false.
let arr = [1,2,3]
console.log(arr.every(e => e > 2)) // false
11. Array.reduce()
Receive a function as accumulator , Every value in the array ( From left to right ) Start to shrink , The final calculation is a value . There is also a corresponding Array.reduceRight() Method , The difference is that this is operated from right to left
let numbers = [65, 44, 12, 4];
function getSum(total, num) {
return total + num;
}
console.log(numbers.reduce(getSum)) // 125
12. Array.filter()
Filter the array , Qualified elements and return a new array .
let arr = [1,2,3]
console.log(arr.filter( e => e < 3)) // [1,2]
13. Array.forEach()
Used to call each element of an array , And pass the element to the callback function . The original array remains the same .( If you print directly Array.forEach, The result is undefined)
let sum = 0
let arr = [1,2,3]
arr.forEach(e => sum += e)
console.log(sum) // 6
14. Array.map()
Returns a new array , The element in the array is the value after the original array element calls the function . The original array remains the same .
let numbers = [4,9,16]
console.log(numbers.map(Math.sqrt)) // [2,3,4]
15. Array.slice(start,end)
from start Start ,end It's over , Less than end; If not end value , from start From the beginning to the end of the array .start You can give a negative value ,-1 Represents the last position of the array ,-2 It means the next to last , And so on , Look before and ignore behind
let arr = [1,2,3,4,5]
console.log(arr.slice(1,3)) // [2,3]
16. Array.splice(index,howmany,arr1,arr2…)
Delete elements and add elements , from index Location start delete howmany Elements , And will arr1、arr2… Data from index Insert... In sequence .howmany by 0 when , The element is not deleted . The original array changes .
let fruits = ["Banana", "Orange", "Apple", "Mango"];
// In position 2, add to 2 Elements , Delete 1 Elements :
fruits.splice(2, 1, "Lemon", "Kiwi");
console.log(fruits) // [Banana,Orange,Lemon,Kiwi,Mango]
17. Array.isArray
Determine whether an object is an array , The return is a Boolean value
18. Array.toString()
Convert array to string
Array weight removal
function unique1(arr) {
return […new Set(arr)]
}
function unique2(arr) {
var obj = {
};
return arr.filter(ele => {
if (!obj[ele]) {
obj[ele] = true;
return true;
}
})
}
function unique3(arr) {
var result = [];
arr.forEach(ele => {
if (result.indexOf(ele) == -1) {
result.push(ele)
}
})
return result;
}
Enter a value and return its data type
function dataType(para) {
return Object.prototype.toString.call(para)
}
边栏推荐
- 职业问诊 | 面试中被问到意向薪资时,该怎么回答?
- PE file-
- E-commerce is popular, how to improve the store conversion rate?
- 职场进阶 | 了解岗位优势三板斧之“进场”
- Akamai acquires linode
- 运动App如何实现端侧后台保活,让运动记录更完整?
- Laravel文档阅读笔记-Adding a Markdown editor to Laravel
- 解读 | 数据分析的发展和演变都经过哪几个阶段?
- Use of axurer9 master
- In one sentence, I will tell you the meaning of select 1, 2 and 3 in SQL injection, and explain the meaning of each part of SQL injection in detail
猜你喜欢

Binomial distribution (a discrete distribution)

ansible生产环境使用场景(七):批量部署elk客户端

16 `bs对象.节点名div.属性contents` children descendants 获取子节点 子孙节点

Zero foundation self-study SQL course | complete collection of date functions in SQL

场景化接口开发利器,金蝶云苍穹新版OpenAPI引擎来了!

硬件开发笔记(七): 硬件开发基本流程,制作一个USB转RS232的模块(六):创建0603封装并关联原理图元器件

Deploy grafana to realize graphical monitoring

User network model and QoE

Lumiprobe lumizol RNA extraction reagent solution

In one sentence, I will tell you the meaning of select 1, 2 and 3 in SQL injection, and explain the meaning of each part of SQL injection in detail
随机推荐
小样本利器2.文本对抗+半监督 FGSM & VAT & FGM代码实现
[linq] execute SQL like in statements using EF to LINQ
Is the VIP securities account of qiniu school really safe and regular? How do I say this?
Appium automated test Jiugongge unlock
ROS 2 Humble Hawksbill 之 f1tenth gym
[software test] 2022 national unified college enrollment examination
rosdep update 使用小鱼fishros解决ros1/ros2问题 2022
Nc1033 palindrome substring of small a (ring, interval DP)
Usage example of qjsonobject
16 `bs对象.节点名div.属性contents` children descendants 获取子节点 子孙节点
flutter通过 GlobalKey 获取界面任意元素坐标尺寸
17 `bs object Node name h3 Parent ` parents get parent node ancestor node
Dart的类扩展、可选类型扩展
Is it safe to open an account on great wisdom
Competition rules for the "network security" event of the secondary vocational group in the skills competition of Guangxi Vocational Colleges in 2022
How to open a safe and reliable securities account in the financial management class of qiniu school?
17 `bs对象.节点名h3.parent` parents 获取父节点 祖先节点
Architecture design of e-commerce secsha system
F1tenth gym of ROS 2 humble hawksbill
Lumiprobe protein labeling research scheme