当前位置:网站首页>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)
}
边栏推荐
- Study on luminiprobe non fluorescent azide -- 3-azido propanol
- 如何制作精美的图片
- PHP login problem
- Un voyage profond d'IA dans Huawei Cloud
- 華為雲的AI深潜之旅
- ADB regular use of work notes
- Sword finger offer:[day 2 linked list (simple)] --- > reverse linked list
- Biovendor free light chain( κ and λ) Test steps of ELISA Kit
- Use of axurer9 option group
- SqlTransaction
猜你喜欢

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

Recommend two high-quality Wallpaper software

BOE was brilliant for the Winter Olympics, but revealed another Chinese technology enterprise dominating the world

电商秒杀系统架构设计

运维体系建设思考 - 稳定性篇

How do independent site sellers efficiently manage complex Facebook pages?

Lumiprobe lumizol RNA extraction reagent solution

PE file-

docker下载Mysql镜像创建数据库链接时候发生密码错误问题

17 `bs对象.节点名h3.parent` parents 获取父节点 祖先节点
随机推荐
彪马携手10KTF Shop启动其迄今为止规模首屈一指的Web3合作项目
【HackTheBox】 meow
Explanation and usage of sqrt() function
Akamai acquires linode
Use of dynamic panels
Anti rabbit dylight 488 abbkine universal immunofluorescence (if) toolbox
Hyperjumptech/grule-rule-engine: rule engine implementation of golang
职业问诊 | 在数据分析面试中,这样做自我介绍才靠谱
Openfire user and group relationship migration
Deploy grafana to realize graphical monitoring
[linq]c list type grouping sum
职场人调研报告:裸辞占比最高的居然是中年人
职业问诊 | 面试时被问到职业规划该怎么回答?
Use of axurer9 option group
6年心得,从功能测试到测试开发,送给在测试路上一路走到黑的你
用指针计算数组的个数
常用工具类与commons 类库
ADB regular use of work notes
场景化接口开发利器,金蝶云苍穹新版OpenAPI引擎来了!
Wechat applet realizes left sliding deletion