js常用数组方法

示例数组创建

let fruits = ['Apple', 'Banana']

console.log(fruits.length)
// 2

循环访问数组forEach

fruits.forEach(function(item, index, array) {
  console.log(item, index)
})
// Apple 0
// Banana 1

将项添加到数组的末尾push

//返回数组的新长度。
let newLength = fruits.push('Orange')
// ["Apple", "Banana", "Orange"]

从数组末尾删除项pop

//此方法更改数组的长度。返回的是删除的元素
let last = fruits.pop() // remove Orange (from the end)
// ["Apple", "Banana"]

从数组的开头删除项shift

//此方法更改数组的长度。返回的是被删除的数组元素
let first = fruits.shift() // remove Apple from the front
// ["Banana"]

将项添加到数组的开头unshift

let newLength = fruits.unshift('Strawberry') // add to the front
// ["Strawberry", "Banana"]

在数组中查找项的索引indexOf

fruits.push('Mango')
// ["Strawberry", "Banana", "Mango"]

let pos = fruits.indexOf('Banana')
// 1

合并两个或多个数组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);
// results in [1, 2, 3, 4, 5, 6, 7, 8, 9]

数组中的所有元素是否都通过由提供的函数实现的测试every

//不会改变调用它的数组 返回布尔值
function isBigEnough(element, index, array) {
  return element >= 10;
}
[12, 5, 8, 130, 44].every(isBigEnough);   // false
[12, 54, 18, 130, 44].every(isBigEnough); // true

将数组中的所有元素更改为静态值 fill

//返回修改后的数组
[1, 2, 3].fill(4)                // [4, 4, 4]
[1, 2, 3].fill(4, 1)             // [1, 4, 4]
[1, 2, 3].fill(4, 1, 2)          // [1, 4, 3]
[1, 2, 3].fill(4, 1, 1)          // [1, 2, 3]
[1, 2, 3].fill(4, 3, 3)          // [1, 2, 3]
[1, 2, 3].fill(4, -3, -2)        // [4, 2, 3]
[1, 2, 3].fill(4, NaN, NaN)      // [1, 2, 3]
[1, 2, 3].fill(4, 3, 5)          // [1, 2, 3]
Array(3).fill(4)                 // [4, 4, 4]
[].fill.call({ length: 3 }, 4)   // {0: 4, 1: 4, 2: 4, length: 3}

// A single object, referenced by each slot of the array:
let arr = Array(3).fill({}) // [{}, {}, {}]
arr[0].hi = "hi"            // [{ hi: "hi" }, { hi: "hi" }, { hi: "hi" }]

创建一个新数组包含通过由提供的函数实现的测试的所有元素。filter

//返回通过测试的元素的新数组。如果没有元素通过测试,则将返回一个空数组
function isBigEnough(value) {
  return value >= 10
}

let filtered = [12, 5, 8, 130, 44].filter(isBigEnough)
// filtered is [12, 130, 44]

返回提供的数组中满足提供的测试函数的第一个元素的值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)         // true
[1, 2, 3].includes(4)         // false
[1, 2, 3].includes(3, 3)      // false
[1, 2, 3].includes(3, -1)     // true
[1, 2, NaN].includes(NaN)     // true
["1", "2", "3"].includes(3)   // false

Array.isArray()方法确定传递的值是否为Array

//如果值是数组则返回true

创建一个新数组,其中填充了在调用数组中的每个元素上调用提供的函数的结果。map

//返回一个新数组,其中每个元素都是回调函数的结果。
let numbers = [1, 4, 9]
let roots = numbers.map(function(num) {
    return Math.sqrt(num)
})
// roots is now     [1, 2, 3]
// numbers is still [1, 4, 9]

Array.of()方法从可变数量的参数创建新实例,而不考虑参数的数量或类型。

//返回新的数组实例
Array.of(1);         // [1]
Array.of(1, 2, 3);   // [1, 2, 3]
Array.of(undefined); // [undefined]

反转数组reverse

//返回反转后的数组
const a = [1, 2, 3];

console.log(a); // [1, 2, 3]

a.reverse();

console.log(a); // [3, 2, 1]

将数组的一部分的浅副本返回到从中选取的新数组对象slice

//返回包含提取的元素的新数组。不会改变原数组
let fruits = ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango']
let citrus = fruits.slice(1, 3)

// fruits contains ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango']
// citrus contains ['Orange','Lemon']

测试数组中是否至少有一个元素通过由提供的函数实现的测试some

//true如果回调函数返回数组中至少一个元素的真值。否则。false
function isBiggerThan10(element, index, array) {
  return element > 10;
}

[2, 5, 8, 1, 4].some(isBiggerThan10);  // false
[12, 5, 8, 1, 4].some(isBiggerThan10); // true

对数组的元素进行排序并返回排序的数组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(); // 'Blue,Humpback,Beluga'
stringArray.sort(); // ['Beluga', 'Blue', 'Humpback']

numberArray.join(); // '40,1,5,200'
numberArray.sort(); // [1, 200, 40, 5]
numberArray.sort(compareNumbers); // [1, 5, 40, 200]

numericStringArray.join(); // '80,9,700'
numericStringArray.sort(); // [700, 80, 9]
numericStringArray.sort(compareNumbers); // [9, 80, 700]

mixedNumericArray.join(); // '80,9,700,40,1,5,200'
mixedNumericArray.sort(); // [1, 200, 40, 5, 700, 80, 9]
mixedNumericArray.sort(compareNumbers); // [1, 5, 9, 40, 80, 200, 700]

通过删除或替换现有元素和/或添加新元素来更改数组的内容splice

//包含已删除元素的数组。如果只删除了一个元素,则返回一个包含一个元素的数组。如果未删除任何元素,则返回空数组。
let myFish = ['angel', 'clown', 'mandarin', 'sturgeon']
let removed = myFish.splice(2, 0, 'drum', 'guitar')

// myFish is ["angel", "clown", "drum", "guitar", "mandarin", "sturgeon"]
// removed is [], no elements removed

let myFish = ['angel', 'clown', 'drum', 'mandarin', 'sturgeon']
let removed = myFish.splice(3, 1)

// myFish is ["angel", "clown", "drum", "sturgeon"]
// removed is ["mandarin"]

返回一个字符串该字符串表示指定的数组元素toString

//返回一个字符串,表示数组的元素。
const array1 = [1, 2, 'a', '1a'];

console.log(array1.toString());
// expected output: "1,2,a,1a"
posted @   七友の  阅读(53)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· AI与.NET技术实操系列(六):基于图像分类模型对图像进行分类
点击右上角即可分享
微信分享提示