JS中常用的API(Array)小整理
new Set() 数据去重
const arr = [3,4,1,5,7,6,8,4,2,9,5,6,8]
let a = new Set(arr)
console.log(a);//3,4,1,5,7,6,8,2,9
sort() 对数组元素进行排序
const arr = [3,4,4,5,4,6,5,7]
console.log(arr.sort((a,b)=> a-b))//[3, 4, 4, 4, 5, 5, 6, 7]
console.log(arr.sort((a,b)=> b-a))//[7, 6, 5, 5, 4, 4, 4, 3]
reverse() 反转数组中的元素(改变原数组)
const arr = [3,4,4,5,4,6,5,7]
console.log(arr.reverse()) //[7, 6, 5, 5, 4, 4, 4, 3]
delete 删除一个数组成员,会形成空位
不影响length属性,同样适用于对象
const arr = [0,1,2]
delete arr[1]
console.log(arr)//[0, empty, 2]
对象使用delete
const obj = {name: '黄油蟹蟹',age:'18',sex:'女'}
delete obj.sex;
console.log(obj);//{name: "黄油蟹蟹", age: "18"}
shift() 把数组的第一个元素从其中删除
并返回第一个元素的值(改变原数组)
const arr = [0,1,2]
const a = arr.shift()
console.log(a,arr)//0 [1, 2]
unshift() 向数组的起始添加一个或者多个元素
并返回新的长度(改变原数组)
const arr = [3,4,4,5,4,6,5,7]
const b = arr.unshift(8,9)
console.log(b) //10
console.log(arr2)//[8, 9, 3, 4, 4, 5, 4, 6, 5, 7]
push() 在数组的末端添加一个或多个元素
并返回添加新元素后数组的长度(改变原数组)
const arr = [3,4,4,5,4,6,5,7]
const c = arr.push(8,9)
console.log(c);//10
console.log(arr)//[3, 4, 4, 5, 4, 6, 5, 7, 8, 9]
toString() 可把值转换为字符串
const arr = [3,4,4,5,4,6,5,7]
console.log(arr.toString())//3,4,4,5,4,6,5,7
function xx(){
return a+b
}
console.log(xx.toString())//function xx(){ return a+b}
concat() 在原始数据尾部添加数据组成新数据
const a1 = [1,2,3]
const a2 = [4,5]
const a3 = a1.concat(a2)
console.log(a3)// [1, 2, 3, 4, 5]
字符串也适用
const x = 'abc'
const y = 'def'
const z = x.concat(y)
console.log(z)//abcdef
join()
以参数作为分隔符,将所有参数组成一个字符串返回(默认逗号分隔)
const arr = [3,4,4,5,4,6,5,7]
console.log(arr.join('-'))//3-4-4-5-4-6-5-7
slice(start,end)
用于提取原来数组的一部分,会返回一个提取的新数组,原数组不变(字符串适用,不包括end)
const arr = [3,4,4,5,4,6,5,7]
console.log(arr.slice(2,5))//[4, 5, 4]
字符串中
const str1 = 'abcdefgh'
const str2 = str1.slice(3,6)
console.log(str2)//def
splice()
用于删除原数组的一部分,并且可以删除的位置添加新的数组成员, 返回值是被删除的数组元素(改变原数组)
splice(t,v,s) t:被删除元素的起始位置;v:被删除元素个数;s:s以及后面的元素为被插入的新元素
const arr = [3,4,4,5,4,6,5,7]
console.log(arr.splice(3,2,12,))//[5, 4]
console.log(arr)//[3, 4, 4, 12, 6, 5, 7]
map()
依次遍历数组成员,根据遍历结果返回一个新数组(不会改变原始数组)
const arr = [3, 4, 4, 5, 4, 6, 5, 7];
console.log(arr.map(item => item * 2));//[6, 8, 8, 10, 8, 12, 10, 14]
forEach()
跟map方法类似,遍历数组,区别是无返回值
const arr = [3, 4, 4, 5, 4, 6, 5, 7];
arr.forEach(function (item, index, arr2) {
console.log(item*2) //6/8/8/10/8/12/10/14
console.log(index) //0/1/2/3/4/5/6/7
})
for in() 跟map方法类似,遍历对象或者数组
但值得注意的是for in 循环返回值都是数据结构的键值名。遍历对象返回的对象key值,遍历数组返回的数组下标(key)
// 对象
const obj = { a: 123, b: 12, c: 2 }
for (let item in obj) {
console.log(item);//a/b/c
}
// 数组
const arr = [3, 2, 4, 5]
for (let a in arr) {
console.log(arr[a]);//3/2/4/5
}
filter()
一个过滤方法,参数是一个函数,所有的数组成员依次执行该函数,返回结果为true的成员组成一个新的数组返回。
const arr = [3, 4, 4, 5, 4, 6, 5, 7];
const b = arr.filter(item => item % 3 > 1)
console.log(b);//[5, 5]
some()&every()
这两个方法类似于“断言”(assert),用于判断数组成员是否符合某种条件
//some()
//some() 方法是只要有一个数组成员的返回值为 true,则返回 true,否则 false;
const arr = [3, 4, 4, 5, 4, 6, 5, 7];
console.log(arr.some(function (item, index, array) {
console.log('item=' + item + ',index=' + index + ',array=' + array);
return item > 3
}));//item=3,index=0,array=3,4,4,5,4,6,5,7
//item=4,index=1,array=3,4,4,5,4,6,5,7
//true
// every()
// every() 方法是需要每一个返回值为 true,才能返回 true,否则为 false;
const arr = [3, 4, 4, 5, 4, 6, 5, 7];
console.log(arr.every(function (item, index, array) {
console.log('item=' + item + ',index=' + index + ',array=' + array);
return item > 3;
}));//item=3,index=0,array=3,4,4,5,4,6,5,7
//false
reduce()
reduce(() => (pre, cur, curIndex, arr), initialValue)
pre: 必填,累计变量;cur:必填,当前变量;curIndex: 可选,
当前位置;arr: 可选,原数组;initialValue: 传递给函数的初始值。
// 使用reduce()实现数组累加
var numbers = [15.5, 2.3, 1.1, 4.7];
function getSum(total, num) {
return total + Math.round(num);
}
console.log(numbers.reduce(getSum, 0))//24
indexOf()
返回给定元素在数组中的第一次出现的位置,如果没有则返回-1
// 数组
const arr = [3, 4, 4, 5, 4, 6, 5, 7];
console.log(arr.indexOf(4));//1
console.log(arr.indexOf('4'));//-1
// 字符串
const string = 'asdfghj'
console.log(string.indexOf('f'));//3
lastIndexOf()
返回给定数组中最后一次出现的位置,没有返回-1
const arr = [3, 4, 4, 5, 4, 6, 5, 7];
console.log(arr.lastIndexOf(4));//4
flat()
接收一个数组,无论这个数组里嵌套了多少数组,flat()最后都会把其边成一个数组(扁平化)
const arr = [[1,2,3],[4,5,[6,7]]];
console.log(arr.flat(2));//[1, 2, 3, 4, 5, 6, 7]
Array.isArray()
用来判断数据是不是一个数组,返回true或false
const arr = [3, 4, 4, 5, 4, 6, 5, 7];
console.log(Array.isArray(arr));//true
find()
返回符合传统测试(函数)条件的数组元素
返回通过测试(函数内判断)的数组的第一个元素的值
const arr = [3, 4, 4, 5, 4, 6, 5, 7];
console.log(arr9.find(item => item > 3));//4
来源 | https://blog.limeichao.cn/article/9796ce43.html