数组常用方法
- push:向数组结尾添加一个或者多个元素,返回数组长度length(改变原数组)
- unshift:向数组开头添加一个或者多个元素,返回数组长度length(改变原数组)
举例1
const arr=[1,2,3]
const length=arr.unshift(1,2)
console.log(length)
//打印得到:4
-
pop:删除数组的最后一个元素,并且返回被删除的元素(改变原数组)
-
shift:删除数组的第一个元素,并且返回被删除的元素(改变原数组)
举例1
const arr=[1,2,3]
const firstElement = arr.shift()
console.log(firstElement )
//打印得到:1
- slice :(返回一个新数组,不会改变原数组)
- 举例1:slice(1, 4) 从数组 arr 中提取了索引 1 到索引 3 的元素(不包括索引 4)
const arr = [1, 2, 3, 4, 5];
const newArr = arr.slice(1, 4);
console.log(newArr); // [2, 3, 4]
- 举例2:slice(1, -1) 表示从数组中截取索引 1 到倒数第2个元素(不包括倒数第1个元素)。
- 举例3:slice(-4, -2) 表示从数组中截取倒数第4个元素到倒数第2个元素(不包括倒数第2个元素)。
- 举例4:slice(-2) 表示从数组中截取倒数第 2 个元素到末尾的所有元素。
- splice:参数(起始索引,删除几个元素(n),…插入的元素(任意元素)) 删除从起始索引开始的,删除n个元素,插入任意元素,返回被删除的元素数组(会改变原数组)
举例1:const arr = [1, 2, 3, 4, 5];
const removed = arr.splice(2, 2, 6, 7);
console.log(arr); // [1, 2, 6, 7, 5]
console.log(removed); // [3, 4]
- reduce :参数(累加器(accumulator),当前值(current value),当前索引(current index),数组(array))
reduce用于数组元素的累积操作,将数组中的每个元素(从左到右)累积成一个单独的值。(不会改变原数组)
示例1: (7-1)数组求和
const numbers=[1,2,2,3]
const sum=numbers.reduce((acc,curr)=>
{
return acc+curr
},0)
console.log(sum);
(7-2)数组去重 (acc是为当前新数组)
const array = [1, 2, 2, 3, 4, 4, 5];
const newArray=array.reduce((acc,curr)=>
{
if(!acc.includes(curr))
acc.push(curr)
return acc
},[ ])
// 输出 [1, 2, 3, 4, 5]
(7-3)数组过滤
const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9];
const newArray=array.reduce((acc,curr)=>
{
if(curr%2===0)
acc.push(curr)
return acc
},[ ])
// 输出 [2, 4, 6, 8]
(7-4)查找最大/最小值
const numbers = [3, 7, 2, 8, 5];
const maxNumbers=numbers.reduce((max,curr)=>(curr>max?curr:max),numbers[0]) //8
const mimNumbers=numbers.reduce((min,curr)=>(curr>min?min:curr),numbers[0]) //2
(7-5) 数据转换(数组转为对象)
const array = ['a', 'b', 'c', 'd'];
const obj=array.reduce((acc,curr,index)=>
{
acc[`key${index+1}`]=curr
return acc
},{});console.log(obj);
//{key1: 'a', key2: 'b', key3: 'c', key4: 'd'}
(7-6)数组分组(数组转为对象,通过category分组)
const data = [
{ id: 1, category: 'A' },
{ id: 2, category: 'B' },
{ id: 3, category: 'A' },
{ id: 4, category: 'C' },
{ id: 5, category: 'B' }
];
const gropObj=data.reduce((acc,curr)=>
{
const category=curr.category
if(!acc[category])
acc[category]=[ ]
acc[category].push(curr)
return acc
},{})
console.log(gropObj);
//{
"A": [
{
"id": 1,
"category": "A"
},
{
"id": 3,
"category": "A"
}
],
"B": [
{
"id": 2,
"category": "B"
},
{
"id": 5,
"category": "B"
}
],
"C": [
{
"id": 4,
"category": "C"
}
]
}
-
map: 高阶函数,接受一个回调函数作为参数(不会改变原数组)
(8-1)常用方式const numbers = [1, 2, 3, 4, 5]; const newArray=numbers.map(num=>num*2) console.log(newArray); // 输出 [2, 4, 6, 8, 10]
(8-2)结合解构赋值提取数组元素的属性
const users = [ { name: 'Alice', age: 30 }, { name: 'Bob', age: 25 } ]; const names=users.map(({name})=>name) console.log(names);
(8-3)使用 map 转换对象数组的属性
const users = [
{ name: 'Alice', age: 30 },
{ name: 'Bob', age: 25 }
];
const newArray=users.map(user=>(
{
name:user.name.toUpperCase(),
//toUpperCase()字符串转大写,toLowerCase() 字符串转小写
age:user.age*2
})
)
console.log(newArray)
//
[
{
"name": "ALICE",
"age": 60
},
{
"name": "BOB",
"age": 50
}
]
- filter: 对符合条件的元素过滤(不会改变原数组)
const numbers = [1, 2, 3, 4, 5];
const evenArray=numbers.filter(num=>num%2===0)
console.log(evenArray) //[2, 4]
- Join: 用于将数组中所有元素连成一个字符串,以中间的参数分隔开来(不会改变原数组)
const fruits = ['apple', 'banana', 'cherry'];
const str=fruits.join(',')
console.log(str) //apple,banana,cherry
- concat:可以接受多个参数,每个参数可以是数组或者元素,用来合并到新的数组中。(不会改变原数组)
示例1:
const arr1=[1,2,3]
const arr2=[4,5,6]
const newArry=arr1.concat(arr2)
console.log(newArry) // [1, 2, 3, 4, 5, 6]
示例2:
const arr1=[1,2,3]
const newArry=arr1.concat(7,8)
console.log(newArry) // [1, 2, 3, 7, 8]
- (12-1)indexOf : 用于在数组中查找指定元素的第一个匹配项,并返回该元素的索引。如果数组中不存在该元素,则返回 -1。(不会改变原数组)
const fruits = ['apple', 'banana', 'cherry'];
const index=fruits.indexOf('banana')
console.log(index) //1
const index1=fruits.indexOf('ban')
console.log(index1) //-1
(12-2)lastIndexOf : 用于在数组中查找指定元素的最后一个匹配项,并返回该元素的索引。如果数组中不存在该元素,则返回 -1。(不会改变原数组)
const fruits = ['apple', 'banana', 'cherry', 'banana'];
const index=fruits.lastIndexOf('banana')
console.log(index) //3
const index1=fruits.indexOf('ban')
console.log(index1) //-1
- forEach: (当前元素,当前元素索引(index),原数组)(不会改变原数组)
const array = ['a', 'b', 'c'];
array.forEach((e,i)=>
{
console.log(e,i)
})
//打印结果:
a 0
b 1
c 2
- reverse():反转数组(会改变原数组)
const numbers = [1, 2, 3];
const newArry=numbers.reverse()
console.log(newArry) // [3, 2, 1]
- sort:对数组的元素进行排序(会改变原数组)
const numbers = [3, 1, 4, 2];
numbers.sort((a,b)=>a-b)
console.log(numbers); //[1, 2, 3, 4]
- includes:用于判断数组是否包含一个特定的值,如果是则返回 true,否则返回 false。(不会改变原数组)
const array = [1, 2, 3, 4, 5];
console.log(array.includes(5)) //true
- every和some:都是用于对数组中的每个元素进行条件判断的方法,它们返回布尔值。
every(当前元素,当前元素的索引(index),数组本身):检查数组中的所有元素是否满足指定条件。
const array = [1, 2, 3, 4, 5];
const allGreaterThanZero =array.every(num=>num>0)
console.log(allGreaterThanZero); // 输出 true,因为数组中的所有元素都大于 0
- some(当前元素,当前元素的索引(index),数组本身):会对数组中的每个元素依次调用这个回调函数,如果有任意一个元素满足指定条件,则返回 true;否则返回 false。
const array = [1, 2, 3, 4, 5];
const hasEvenNumber = array.some(function(element) {
return element % 2 === 0;
});
console.log(hasEvenNumber); // 输出 true,因为数组中包含偶数
- find:返回数组中满足提供的测试函数的第一个元素的值。
const array = [1, 2, 3, 4, 5];
const result=array.find(e=>e>3)
console.log(result)
- finIndex() 可以用于返回数组中满足提供的测试函数的第一个元素的索引。
const array = [10, 20, 30, 40, 50];
const index=array.findIndex(e=>e>30)
console.log(index) //3
- toString:将数组转换为字符串
const array = [1, 2, 3, 4, 5];
const str= array.toString()
console.log(str) // '1,2,3,4,5'