有一些数组方法是ECMAScript新增的,一定要注意浏览器的兼容!!
Array对象属性:constructor
返回对创建此对象的函数引用const arr = [1, 2, 4, 5, 6, 9, 15]length
console.log(arr.constructor) //输出为 ƒ Array() { [native code] }
const arr = [1, 2, 4]下文的箭头函数的解释为:x => x*1 == function(x){return x*1} 如果是有多个参数则:(x,y) => x*y == function(x,y){return x*y} 。
console.log(arr.length) //输出为 3
我这只是为了简写,并不代表适用。
Array对象方法:
第一个参数定义添加新元素的位置,以拼接的方式,第二个参数是定义应删除多少个元素
第一个参数定义删除元素的位置,第二个参数是结束位置。
注意:S为大写。
(排序的依照我还搞不清楚....)
注意:用函数判断、如果没有符合条件的元素则返回undefined。
注意:后面的元素会被位移出数组,慎用!
shift()
const myArray = [3, 1, 5, 2, 6]
console.log(myArray.shift()) //输出为 3
unshift()
const myArray = [3, 1, 5, 2, 6]
console.log(myArray.unshift(1,3,2323)) //输出为 [1, 3, 2323, 3, 1, 5, 2, 6]
splice()
const myArray = [3, 1, 5, 2, 6]
console.log(myArray.splice(1,1,'浮云共我归')) //输出为 [3, "浮云共我归", 5, 2, 6]
slice()
const arr2 = [1, 2, 3, 4, 5, 6, 7, 8, 9]
console.log(arr2.slice(2, 4)) //输出为 (2) [3, 4]
pop()
const arr2 = [1, 2, 3, 4, 5, 6, 7, 8, 9]
console.log(arr2.pop()) //输出为 9
push()
const arr2 = [1, 2, 3, 4]
console.log(arr2.push('233','333'))
console.log(arr2) //输出为 6 、 (6) [1, 2, 3, 4, "233", "333"]
toString()
const myArray = [3, 1, 5, 2, 6]
console.log(myArray.toString()) //输出为 3,1,5,2,6
concat()
const myArray = [3, 1, 5, 2, 6]
const MyArray = [66,77]
const result = myArray.concat(MyArray)
console.log(result.concat('add')) //输出为 [3, 1, 5, 2, 6, 66, 77, "add"]
sort()
const arr2 = [6, 2, '云', 1, 4, 'a']
const result = arr2.sort((x, y) => {
if (x > y) {
return 1
}
if (x < y) {
return -1
}
return 0
})
console.log(result) //输出为 (6) [1, 2, 4, 6, "a", "云"]
valueOf()
const arr2 = [6, 2, '云', 1]
console.log(arr2.valueOf()) // 输出为 (4) [6, 2, "云", 1]
join() (结合split()方法会有意想不到的结果,而且我发现)
const arr2 = ['浮','云','共','我','归']console.log(arr2.join('')) // 输出为 浮云共我归
//用上这行arr2.join('').split('') 又变回原样了
//split()里不加值 输出: ["浮云共我归"]
isArray()
const myArray = [3, 1, 5, 2, 6]
console.log(Array.isArray(myArray)) //输出为 true
includes()
const arr2 = ['浮','云','共','我','归']
console.log(arr2.includes('云','浮')) // 输出为 true
lastIndexOf()
const arr2 = ['浮','云','共','我','归']
arr2.shift()
onsole.log(arr2.lastIndexOf('云')) // 输出为 0
find()
const myArray = [3, 1, 5, 2, 6]
const result = myArray.find(x => x > 5)
console.log(result) //输出为 6
indexOf()
const myArray = [3, 1, 5, 2, 6]
console.log(myArray.indexOf(6)) //输出为 4
copyWithin()
const myArray = [1, 2,'云', 3, 4, 5] const result = myArray.copyWithin(2,0)
console.log(result)
//输出为 (6) [1, 2, 1, 2, "云", 3]
Array对象-高阶函数
注意:不会对空元素进行检测,不会改变原数组
此方法接收一个函数作为累加器,数组中的每个元素从左到右相加,最终计算为一个值。
可用于函数的compose。
注意:对空数组不会执行回调函数。
reduceRight()从右往左累加
filter()
// 过滤偶数
const arr = [1, 2, 4, 5, 6, 9, 15]
myarr = arr.filter((x) => x % 2 !== 0)
console.log(myarr)
reduce()
//将元素累加至最终全部相加的值,输出结果为42
const Nums = [1, 5, 1, 5, 10, 20]
const total = Nums.reduce((preValue, n) => {
return preValue + n
}, 0)
//后面0表示初始值,正数则在结果值上加,负数反之
map() (这个方法的用法也很多)
const myArray = [1, 2, 3, 4, 5]数组遍历方法
console.log(myArray.map((function(x) {
return x + 1
}))) // 输出为 (5) [2, 3, 4, 5, 6]
keys()方法也和这个差不多
forEach
const myArray = [1, 2,'云', 3, 4, 5] //element是当前元素,index是元素索引,array是当前元素所属的数组对象
myArray.forEach(function(element, index,array) {
console.log(element,index,array)
}) /* 输出为:1 0 (6) [1, 2, "云", 3, 4, 5]
2 1 (6) [1, 2, "云", 3, 4, 5]
云 2 (6) [1, 2, "云", 3, 4, 5]
3 3 (6) [1, 2, "云", 3, 4, 5]
4 (6) [1, 2, "云", 3, 4, 5]
5 5 (6) [1, 2, "云", 3, 4, 5] */
for of
const myArray = [1, 2,'云', 3, 4, 5] for(result of myArray){
console.log(result)
}
//输出为 1 2 云 3 4 5
for in
const myArray = [1, 2,'云', 3, 4, 5] for(result in myArray){数组的一些常用方法
console.log(result)
}
//输出为 0 1 2 3 4 5
去重
const myArray = [3, 1, 5, 1, 6]
const result = Array.from(new Set(myArray))
console.log(result) //输出为 [3,1,5,6]
字符串转数组 (数组转字符串可用toString()的方法)
const myArray = '浮云共我归'
const result = Array.from(myArray)
console.log(result) //输出为 ["浮,云,共,我,归"] //如果去掉from 输出为 ["浮云共我归"]
将首字母变为大写,其余小写
const arr = ['adam', 'LISA', 'barT']
const arr2= ['Adam', 'Lisa', 'Bart'] function result(arr) {
return arr.map(function(x) {
x = x.toLowerCase()
//toUpperCase()将元素转化为大写,substring()提取指定下标里的元素后面的字符
x = x[0].toUpperCase() + x.substring(1)
return x
})
}
console.log(result(arr).toString() === arr2.toString()) //输出为 true 转换小写的方法是 toLowperCase()
用map创建键值对集合 && 元素乘与自身
const arr1 = new Map([
['lai', 199],
['quan', 'map'],
['feng', 10]
])
console.log(arr1)
//输出为 Map(3) {"lai" => 199, "quan" => "map", "feng" => 10} const arr2 = [1, 2, 3, 4, 5, 6, 7, 8, 9]
const result = arr2.map(x => x * x)
console.log(result)
//输出为 (9) [1, 4, 9, 16, 25, 36, 49, 64, 81]
找出数组内最大的数字
const myArray = [1, 2, 3, 4, 5] const result = Math.max(...myArray)
console.log(result)
//输出为 5//最小值的话换成min即可
去除数组内的空格 缺点是数组内不能包含数字
const myArray = ['a', ' ', null, '', undefined, 'b', 'c',' ']
const result = myArray.filter((z) => z && z.trim())
console.log(result) //输出为 (3) ["a", "b", "c"]
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)