Array(源自 mozilla developer network, 自带实例)

这篇博客详细介绍了JavaScript中的Array对象,包括如何创建、访问数组元素、遍历数组、向数组添加和删除元素,以及各种实例方法如`copyWithin()`、`fill()`、`push()`等。还涵盖了遍历方法如`forEach()`、`every()`、`some()`、`filter()`等,以及查找和修改数组元素的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Array

Array对象是一个用于创建数组的全局对象。

create an Array

var fruits = ["Apple", "Banana"];

console.log(fruits.length);
// 2

Access (index into) an Array item

var first = fruits[0];
// Apple

var last = fruits[fruits.length - 1];
// Banana

Loop over an Array

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

Add to the end of an Array

var newLength = fruits.push("Orange");
// ["Apple", "Banana", "Orange"]

Removed from the end of an Array

var last = fruits.pop(); // remove Orange (from the end)
// ["Apple", "Banana"];

Add to the front of an Array

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

Find the index of an item in the Array

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

var pos = fruits.indexOf("Banana");
// 1

Remove an item by Index Position

var removedItem = fruits.splice(pos, 1); // this is how to remove an item,

// ["Strawberry", "Mango"]

Remove items from an Index Position

var removedItems = fruits.splice(pos, n); // this is how to remove items, n defines the number of items to be removed,
                                          // from that position onward to the end of array.
// let, n = 1;

// ["Strawberry"]

Copy an Array

var shallowCopy = fruits.slice(); // this is how to make a copy
// ["Strawberry"]

Syntax

[element0, element1, ..., elementN]
new Array(element0, element1[, ...[, elementN]])
new Array(arrayLength)

Description

js数组是一种list-like对象,它的原型方法可以进行遍历和修改操作。数组包含的元素个数和类型都不是固定的。

Methods

Array.from()

通过一个类数组对象或者可遍历对象创建数组

Array.isArray()

判断变量是否是Array的实例

Array.of()

通过数量类型不定的一组参数创建一个新的数组实例(与new Array或者Array有区别)

Polyfill 兼容实现
if(!Array.of) {
    Array.of = function() {
        return Array.prototype.slice.call(arguments);
    };
}

Array instance Methods

以下都是数组的修改方法

Array.prototype.copyWithin()

The copyWithin() method shallow copies part of an array to another location in the same array and returns it, without modifying its size.

arr.copyWithin(target[, start[, end]])

若target, start, end为负值,将被处理为arr.length

target. Zero based index at which to copy the sequence to.

Zero based index at which to start copying elements from

Zero based index at which to end copying elements from.

Array.prototype.fill()

The fill() method fills all the elements of an array from a start index to an end index with a static value.

arr.fill(value[, start = 0[, end = this.length]])

[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, -2);       // [4, 2, 3]
[1, 2, 3].fill(4, NaN, NaN);     // [1, 2, 3]
Array(3).fill(4);                // [4, 4, 4]
[].fill.call({ length: 3 }, 4);  // {0: 4, 1: 4, 2: 4, length: 3}

Array.prototype.pop()

Array.prototype.push()

Array.prototype.reverse()

Array.prototype.shift()

Array.prototype.sort()

arr.sort([compareFunction])

compareFunction可选。如果没有指定compareFunction,会试图把元素转换为字符串,并比较它们的unicode code point值.

Array.prototype.splice()

Array.prototype.unshift()

以下方法并不会修改数组,但会返回一些数组的元素

Array.prototype.concat()

Array.prototype.join()

Array.prototype.slice()

Array.prototype.toString()

Array.prototype.toLocaleString()

Array.prototype.indexOf()

Array.prototype.lastIndexOf()

以下是遍历方法

Array.prototype.forEach()

arr.forEach(callback[, thisArg])

callback参数 currentValue, index, array

thisArray可选。调用callback时的this

本方法不会遍历已被删掉的元素或者没有初始化的元素(稀疏数组)

不像map(), reduce(),本方法返回undefined,因此它不能链式调用.

Array.prototype.entries()

arr.entries()

返回一个Array iterator对象

var arr = ['a', 'b', 'c'];
var eArr = arr.entries();

console.log(eArr.next().value); // [0, 'a']
console.log(eArr.next().value); // [1, 'b']
console.log(eArr.next().value); // [2, 'c']
var arr = ['a', 'b', 'c'];
var eArr = arr.entries();

for (let e of eArr) {
  console.log(e);
}

Array.prototype.every()

arr.every(callback[, thisArg])

被删除的元素不会被访问

判断每一个元素是否通过提供的function

callback回调参数: currentValue(必须) index(可选) array(可选)

thisAr可选。执行回调时的this

每一次回调都返回truthy value时,该函数返回true,否则false

var arr = [1, 2, 3, , , 6];
arr.every(function(value) {
    if(value > 0) {
        return true;
    } else {
        return false;
    }
}); //true

Array.prototype.some()

Array.prototype.filter()

var new_array = arr.filter(callback[, thisArg])

被删除的元素不会被访问

创建一个新数组,它包含所有通过指定函数的元素

var arr = [1, 2, 3, , , 6];
arr.filter(function(value) {
    if(value > 2) {
        return true;
    }
}); // 3, 6

Array.prototype.find()

arr.find(callback[, thisArg])

被删除的元素不会被访问

如果数组中的一个元素能通过指定函数,则立即返回该值

类似地, findIndex返回元素的下标
var arr = [1, 2, 3, , , 6];
arr.find(function(value) {
if(value > 2) {
return true;
}
}); // 3

Array.prototype.findIndex()

Array.prototype.keys()

arr.keys()

返回一个Array iterator对象

Basic usage
var arr = ["a", "b", "c"];
var iterator = arr.keys();

console.log(iterator.next()); // { value: 0, done: false }
console.log(iterator.next()); // { value: 1, done: false }
console.log(iterator.next()); // { value: 2, done: false }
console.log(iterator.next()); // { value: undefined, done: true }
Key iterator doesn't ignore holes
var arr = ["a", , "c"];
var sparseKeys = Object.keys(arr);
var denseKeys = [...arr.keys()];
console.log(sparseKeys); // ['0', '2']
console.log(denseKeys);  // [0, 1, 2]

Array.prototype.map()

var new_array = arr.map(callback[, thisArg])

被删除的元素也会被访问

根据指定方法返回的结果创建一个新数组.

var arr = [1, 2, 3, , , 6];
arr.map(function(value, index) {
    return {
        index: index,
        value: value
    }
});
/*[ { index: 0, value: 1 },
  { index: 1, value: 2 },
  { index: 2, value: 3 },
  ,
  ,
  { index: 5, value: 6 } ]*/

Array.prototype.reduce()

arr.reduceRight(callback[, initialValue])

将函数返回值作为一个叠加器,从左往右,依次叠加,直到只有一个值

callback回调参数previousValue, currentValue, index, array

initialValue可选,第一次调用callback时的previousValues值

var arr = [1, 2, 3, , , 6];
arr.reduce(function(prev, cur) {
    return prev + cur;
}); // 12

Array.prototype.reduceRight()

软件介绍: ArrayNetworksL3Setu是移动代理点客户端软件,也就是常说的那个红A。需要安装这个软件后才能登陆。BOSS 客户端更新说明为了解决现有BOSS系统BUG,现在需要升级各代办点终端的SSL 的插件,具体插件步骤如下:1.将附件中名称为:“ArrayNetworksL3OnlyWebSetup.zip”的安装包拷贝到代办终端上。 2.在代办终端上解压该文件3.点击“setup.exe”4.一步步安装首先remove现有的插件。点击“next”,点击“finish”,再点击“setup.exe”,点击“finish”完成安装。完成后开始使用,打开IE浏览器。输入移动 IP地址。IE版本可能会出现,点击“允许”,当右下角出现“A” 上面出现8.4.6.80确认为新的插件版本。出现红A,没有任何报错就表示安装正常。-----------------------------------------------------------------------------------------------------如果安装有问题或者不能正常访问,请单独安装客户端。安装的文件名称ArrayNetworksL3SetupX32.zip,ArrayNetworksL3SetupX64.zip请对应系统的版本安装1查看自己的系统的版本,32位,64位2.“计算机”-->“属性”查看自己的是32位的还是64位的操作系统。请对应版本安装。4.安装客户端软件的步骤,首先解压文件。点击 “setup.exe”安装完成。打开IE登陆SSL 如重启失败请重置浏览器的高级设置。点击---“还远高级设置”---“确定”再次登陆
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值