示例数组创建
let fruits = [ 'Apple' , 'Banana' ]
console. log ( fruits. length)
循环访问数组forEach
fruits. forEach ( function ( item, index, array ) {
console. log ( item, index)
} )
将项添加到数组的末尾push
let newLength = fruits. push ( 'Orange' )
从数组末尾删除项pop
let last = fruits. pop ( )
从数组的开头删除项shift
let first = fruits. shift ( )
将项添加到数组的开头unshift
let newLength = fruits. unshift ( 'Strawberry' )
在数组中查找项的索引indexOf
fruits. push ( 'Mango' )
let pos = fruits. indexOf ( 'Banana' )
合并两个或多个数组concat
const num1 = [ 1 , 2 , 3 ] ;
const num2 = [ 4 , 5 , 6 ] ;
const num3 = [ 7 , 8 , 9 ] ;
const numbers = num1. concat ( num2, num3) ;
console. log ( numbers) ;
数组中的所有元素是否都通过由提供的函数实现的测试every
function isBigEnough ( element, index, array ) {
return element >= 10 ;
}
[ 12 , 5 , 8 , 130 , 44 ] . every ( isBigEnough) ;
[ 12 , 54 , 18 , 130 , 44 ] . every ( isBigEnough) ;
将数组中的所有元素更改为静态值 fill
[ 1 , 2 , 3 ] . fill ( 4 )
[ 1 , 2 , 3 ] . fill ( 4 , 1 )
[ 1 , 2 , 3 ] . fill ( 4 , 1 , 2 )
[ 1 , 2 , 3 ] . fill ( 4 , 1 , 1 )
[ 1 , 2 , 3 ] . fill ( 4 , 3 , 3 )
[ 1 , 2 , 3 ] . fill ( 4 , - 3 , - 2 )
[ 1 , 2 , 3 ] . fill ( 4 , NaN , NaN )
[ 1 , 2 , 3 ] . fill ( 4 , 3 , 5 )
Array ( 3 ) . fill ( 4 )
[ ] . fill . call ( { length : 3 } , 4 )
let arr = Array ( 3 ) . fill ( { } )
arr[ 0 ] . hi = "hi"
创建一个新数组包含通过由提供的函数实现的测试的所有元素。filter
function isBigEnough ( value ) {
return value >= 10
}
let filtered = [ 12 , 5 , 8 , 130 , 44 ] . filter ( isBigEnough)
返回提供的数组中满足提供的测试函数的第一个元素的值find
//返回数组中满足提供的测试函数的第一个元素的值。否则,将返回undefined。
const inventory = [
{name: 'apples', quantity: 2},
{name: 'bananas', quantity: 0},
{name: 'cherries', quantity: 5}
];
function isCherries(fruit) {
return fruit.name === 'cherries';
}
console.log(inventory.find(isCherries));
// { name: 'cherries', quantity: 5 }
确定数组是否在其条目中包含某个值include
[ 1 , 2 , 3 ] . includes ( 2 )
[ 1 , 2 , 3 ] . includes ( 4 )
[ 1 , 2 , 3 ] . includes ( 3 , 3 )
[ 1 , 2 , 3 ] . includes ( 3 , - 1 )
[ 1 , 2 , NaN ] . includes ( NaN )
[ "1" , "2" , "3" ] . includes ( 3 )
Array.isArray()
方法确定传递的值是否为Array
创建一个新数组 ,其中填充了在调用数组中的每个元素上调用提供的函数的结果。map
let numbers = [ 1 , 4 , 9 ]
let roots = numbers. map ( function ( num ) {
return Math. sqrt ( num)
} )
Array.of()
方法从可变数量的参数创建新实例,而不考虑参数的数量或类型。
Array. of ( 1 ) ;
Array. of ( 1 , 2 , 3 ) ;
Array. of ( undefined ) ;
反转数组reverse
const a = [ 1 , 2 , 3 ] ;
console. log ( a) ;
a. reverse ( ) ;
console. log ( a) ;
将数组的一部分的浅副本返回到从中选取的新数组对象slice
let fruits = [ 'Banana' , 'Orange' , 'Lemon' , 'Apple' , 'Mango' ]
let citrus = fruits. slice ( 1 , 3 )
测试数组中是否至少有一个元素通过由提供的函数实现的测试some
function isBiggerThan10 ( element, index, array ) {
return element > 10 ;
}
[ 2 , 5 , 8 , 1 , 4 ] . some ( isBiggerThan10) ;
[ 12 , 5 , 8 , 1 , 4 ] . some ( isBiggerThan10) ;
对数组的元素进行排序并返回排序的数组sort
let stringArray = [ 'Blue' , 'Humpback' , 'Beluga' ] ;
let numericStringArray = [ '80' , '9' , '700' ] ;
let numberArray = [ 40 , 1 , 5 , 200 ] ;
let mixedNumericArray = [ '80' , '9' , '700' , 40 , 1 , 5 , 200 ] ;
function compareNumbers ( a, b ) {
return a - b;
}
stringArray. join ( ) ;
stringArray. sort ( ) ;
numberArray. join ( ) ;
numberArray. sort ( ) ;
numberArray. sort ( compareNumbers) ;
numericStringArray. join ( ) ;
numericStringArray. sort ( ) ;
numericStringArray. sort ( compareNumbers) ;
mixedNumericArray. join ( ) ;
mixedNumericArray. sort ( ) ;
mixedNumericArray. sort ( compareNumbers) ;
通过删除或替换现有元素和/或添加新元素来更改数组的内容splice
let myFish = [ 'angel' , 'clown' , 'mandarin' , 'sturgeon' ]
let removed = myFish. splice ( 2 , 0 , 'drum' , 'guitar' )
let myFish = [ 'angel' , 'clown' , 'drum' , 'mandarin' , 'sturgeon' ]
let removed = myFish. splice ( 3 , 1 )
返回一个字符串该字符串表示指定的数组元素toString
const array1 = [ 1 , 2 , 'a' , '1a' ] ;
console. log ( array1. toString ( ) ) ;
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· AI与.NET技术实操系列(六):基于图像分类模型对图像进行分类