JavaScript 数组

发布于 2024-08-07 20:11:10 字数 5190 浏览 10 评论 0

JavaScript 数组是一个对象,代表一组相似类型的元素。

使用 JavaScript 构造数组的 3 种方法

  • 按数组字面量
  • 通过直接创建 Array 的实例(使用 new 关键字)
  • 通过使用 Array 构造函数(使用 new 关键字)

1) JavaScript 数组字面量

使用数组字面量创建数组的语法如下:

var arrayname=[value1,value2.....valueN];

如您所见,值包含在[]内,并以,(逗号) 分隔。

让我们看一下在 JavaScript 中创建和使用数组的简单示例。


.length 属性返回数组的长度。

上面例子的输出

Sonoo
Vimal
Ratan

2) 直接使用 JavaScript 数组(新关键字)

直接给出创建数组的语法如下:

var arrayname=new Array();

在这里,new 关键字用于创建数组实例。

让我们看看直接创建数组的示例。


上面例子的输出

Arun
Varun
John

3) JavaScript 数组构造函数(新关键字)

在这里,您需要通过在构造函数中传递参数来创建数组的实例,这样我们就不必显式提供值。

下面给出了通过数组构造函数创建对象的示例。


上面例子的输出

Jai
Vijay
Smith

JavaScript 数组方法

让我们看一下 JavaScript 数组方法及其说明的列表。

MethodsDescription
concat()It returns a new array object that contains two or more merged arrays.
copywithin()It copies the part of the given array with its own elements and returns the modified array.
entries()It creates an iterator object and a loop that iterates over each key/value pair.
every()It determines whether all the elements of an array are satisfying the provided function conditions.
flat()It creates a new array carrying sub-array elements concatenated recursively till the specified depth.
flatMap()It maps all array elements via mapping function, then flattens the result into a new array.
fill()It fills elements into an array with static values.
from()It creates a new array carrying the exact copy of another array element.
filter()It returns the new array containing the elements that pass the provided function conditions.
find()It returns the value of the first element in the given array that satisfies the specified condition.
findIndex()It returns the index value of the first element in the given array that satisfies the specified condition.
forEach()It invokes the provided function once for each element of an array.
includes()It checks whether the given array contains the specified element.
indexOf()It searches the specified element in the given array and returns the index of the first match.
isArray()It tests if the passed value ia an array.
join()It joins the elements of an array as a string.
keys()It creates an iterator object that contains only the keys of the array, then loops through these keys.
lastIndexOf()It searches the specified element in the given array and returns the index of the last match.
map()It calls the specified function for every array element and returns the new array
of()It creates a new array from a variable number of arguments, holding any type of argument.
pop()It removes and returns the last element of an array.
push()It adds one or more elements to the end of an array.
reverse()It reverses the elements of given array.
reduce(function, initial)It executes a provided function for each value from left to right and reduces the array to a single value.
reduceRight()It executes a provided function for each value from right to left and reduces the array to a single value.
some()It determines if any element of the array passes the test of the implemented function.
shift()It removes and returns the first element of an array.
slice()It returns a new array containing the copy of the part of the given array.
sort()It returns the element of the given array in a sorted order.
splice()It add/remove elements to/from the given array.
toLocaleString()It returns a string containing all the elements of a specified array.
toString()It converts the elements of a specified array into string form, without affecting the original array.
unshift()It adds one or more elements in the beginning of the given array.
values()It creates a new iterator object carrying values for each index in the array.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

关于作者

甜点

暂无简介

0 文章
0 评论
21 人气
更多

推荐作者

花开柳相依

文章 0 评论 0

zyhello

文章 0 评论 0

故友

文章 0 评论 0

对风讲故事

文章 0 评论 0

Oo萌小芽oO

文章 0 评论 0

梦明

文章 0 评论 0

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文