登录
首页 >  文章 >  前端

JavaScript 数组

来源:dev.to

时间:2024-08-02 08:21:39 137浏览 收藏

学习知识要善于思考,思考,再思考!今天golang学习网小编就给大家带来《JavaScript 数组》,以下内容主要包含等知识点,如果你正在学习或准备学习文章,就都不要错过本文啦~让我们一起来看看吧,能帮助到你就更好了!

JavaScript 数组

什么是数组?

数组是一种存储有序元素集合的数据结构。在 javascript 中,数组被归类为一种特殊类型的对象,可以存储数字、字符串、对象或其他数组。数组中的元素括在方括号 [ ] 中并使用从零开始的索引。从零开始的索引意味着数组的第一个元素的索引为 0,第二个元素的索引为 1,依此类推。

const names = ["david", "hannah", "william"];
console.log(names[0]); // returns the first element
// returns "david"
console.log(names[1]); // returns the second element
// returns "hannah"
console.log(names[2]); // returns the third element
// returns "william"

如何修改或操作数组?

数组中元素的索引

可以通过为空索引赋值来将新元素添加到数组中。

names[3] = "eric";
console.log(names);
// returns ["david", "hannah", "william", "eric"]

可以通过为现有索引重新分配新值来修改数组中的元素。

names[1] = "juniper";
console.log(names);
// returns ["david", "juniper", "william", "eric"]

数组方法

数组还可以使用数组方法进行修改或操作,例如 'push'、'pop'、'unshift'、'shift'、'slice' 和 'splice'。

'推()'

'push' 方法接受一个或多个元素作为参数,将元素添加到数组末尾,并返回修改后的数组的长度。

names.push("bob");
// returns 5 
console.log(names);
// returns ["david", "juniper", "william", "eric", "bob"]

'流行音乐()'

'pop' 方法不带参数,删除数组的最后一个元素,并返回删除的元素。

names.pop();
// returns "bob"
console.log(names);
// returns ["david", "juniper", "william", "eric"]

'取消移位()'

'unshift' 方法接受一个或多个元素作为参数,将元素添加到数组的开头,并返回修改后的数组的长度。

names.unshift("jack", "jane");
// returns 6
console.log(names);
// returns ["jack", "jane", "david", "juniper", "william", "eric"]

'转移()'

“shift”方法不带参数,删除数组的第一个元素,并返回删除的元素。

names.shift();
// returns "jack"
console.log(names);
// returns ["jane", "david", "juniper", "william", "eric"]

'片()'

“slice”方法采用两个可选参数(startindex、endindex),并返回一个新数组,其中包含从 startindex 到(但不包括)原始数组的 endindex 的元素。
如果省略 startindex,则使用 0。
如果省略 endindex,则使用数组长度。负索引号可用于从数组末尾开始倒数。

names.slice(1, 3);
// returns ["david", "juniper"]
names.slice(3);
// returns ["juniper", "william", "eric"]
names.slice(-2, 1);
// returns ["william", "eric", "jane"]
names.slice();
// returns ["jane", "david", "juniper", "william", "eric"]

'拼接()'

“splice”方法接受一个或多个参数(startindex、deletecount、element1、element2...)并返回一个包含所有删除元素的新数组。从startindex 开始,删除deletecount 个元素,并将以下元素参数添加到从startindex 开始的数组中。如果省略deletecount,则删除从startindex 到数组末尾的所有元素。如果省略元素参数,则不会添加任何元素。

names.splice(0, 1, "joe", "alex"); 
// returns ["jane"]
console.log(names);
// returns ["joe", "alex", "david", "juniper", "william", "eric"]
names.splice(1, 4);
// returns ["alex", "david", "juniper", "william"]
console.log(names);
// returns ["joe", "eric"]
names.splice(0, 0, "bob", "frank", "maria")
// returns []
console.log(names);
// returns ["joe", "bob", "frank", "maria", "eric"]

由于“push”、“pop”、“unshift”、“shift”和“splice”修改了原始数组,因此它们被归类为破坏性方法。 “切片”方法使原始数组保持完整,因此它被归类为非破坏性。

扩展运算符 '...'

要无损地向数组添加元素或复制数组,可以使用扩展运算符。扩展运算符将数组扩展为其元素。

const array = [1, 2, 3];
const newarray = [0, ...array, 4, 5];
// ...array spreads [1, 2, 3] into 1, 2, 3
console.log(newarray);
// returns [1, 2, 3, 4, 5]

如果没有扩展运算符,原始数组将嵌套在新数组中。

const array = [1, 2, 3];
const newarray = [0, array, 4, 5];
console.log(newarray);
// returns [0, [1, 2, 3], 4, 5];

迭代数组方法

迭代数组方法对数组中的每个元素调用提供的函数并返回一个值或新数组。使用三个参数调用提供的函数:当前元素、当前元素的索引以及调用该方法的原始数组。

function callbackfunction (currentelement, currentindex, originalarray) {
// function body
}

迭代数组方法的一些示例是:“find”、“filter”、“map”和“reduce”。

'寻找()'

‘find’方法接受一个函数作为参数,并返回数组中满足函数条件的第一个元素。

const numbers = [5, 10, 15, 20, 25];
numbers.find(number => number > 15);
// returns 20;

'筛选()'

“filter”方法与“find”方法类似,但返回满足给定函数条件的所有元素的数组。

const numbers = [5, 10, 15, 20, 25];
numbers.filter(number => number > 15);
// returns [20, 25];

'地图()'

“map”方法返回一个新数组,其中包含对原始数组中每个元素调用该函数的结果。

const numbers = [1, 2, 3, 4, 5];
numbers.map(number => number * number);
// returns [1, 4, 9, 16, 25]

'减少()'

“reduce”方法接受一个函数和一个初始值作为参数。提供的函数接收四个参数:累加器、当前值、当前索引和原始数组。提供的初始值是数组第一个元素的累加器的值。每个元素的函数结果用作数组中下一个元素的累加器的值。如果未提供初始值,则将累加器设置为数组的第一个元素,并从第二个元素开始调用回调函数。

const numbers = [1, 2, 3, 4, 5]
numbers.reduce(((acc, number) => acc + number), 0);
// returns 15

今天关于《JavaScript 数组》的内容介绍就到此结束,如果有什么疑问或者建议,可以在golang学习网公众号下多多回复交流;文中若有不正之处,也希望回复留言以告知!

声明:本文转载于:dev.to 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>