外贸网站建设升上去,html制作个人简历代码案例,网络规划设计师视频网盘,重庆安全员c证查询官网JavaScript 中的 Array#xff08;数组#xff09;对象是一种用来存储一系列值的容器#xff0c;它可以包含任意类型的数据#xff0c;包括数字、字符串、对象等等。通过使用数组对象#xff0c;我们可以轻松地组织和处理数据#xff0c;以及进行各种操作#xff0c;比如…JavaScript 中的 Array数组对象是一种用来存储一系列值的容器它可以包含任意类型的数据包括数字、字符串、对象等等。通过使用数组对象我们可以轻松地组织和处理数据以及进行各种操作比如查找、排序、过滤、迭代等等。
创建数组
在 JavaScript 中我们可以通过多种方式来创建一个数组对象。
2.1 直接定义
最简单的创建数组的方式就是直接定义数组变量并用方括号 [] 括起来的一组值来初始化数组例如
let arr [1, 2, 3, 4, 5];在这个例子中我们定义了一个包含 5 个元素的数组分别是 1、2、3、4、5。
2.2 构造函数
另一种创建数组的方式是使用 Array 构造函数来创建数组例如
let arr new Array(1, 2, 3, 4, 5);这个例子和上面的例子等价它使用了 Array 构造函数来创建一个数组对象并初始化了它的元素。
除了传入一组初始值外我们还可以使用一个数字参数来指定数组的长度例如
let arr new Array(5);这个例子创建了一个长度为 5 的数组但是数组中的元素都是 undefined。
2.3 Array.of 和 Array.from
在 ES6 中JavaScript 提供了两个新的数组方法 Array.of 和 Array.from用来创建数组。
Array.of 方法可以用来创建一个包含任意数量元素的数组例如
let arr Array.of(1, 2, 3, 4, 5);这个例子和直接定义数组变量的方式等价。
Array.from 方法可以用来将一个类数组对象或可迭代对象转换成数组例如
let arr Array.from(hello);
// arr: [h, e, l, l, o]这个例子将一个字符串转换成了一个包含每个字符的数组。
数组元素的访问和修改
数组中的元素可以通过索引来访问和修改。在 JavaScript 中数组的索引从 0 开始也就是第一个元素的索引是 0第二个元素的索引是 1以此类推。
3.1 访问元素
我们可以使用方括号 [] 操作符来访问数组中的元素例如
let arr [1, 2, 3, 4, 5];
console.log(arr[0]); // 1
console.log(arr[2]); // 3这个例子访问了数组中的第一个元素和第三个元素。
3.2 修改元素
我们也可以使用方括号操作符来修改数组中的元素例如
let arr [1, 2, 3, 4, 5];
arr[0] 6;
arr[2] 7;
console.log(arr); // [6, 2, 7, 4, 5] 这个例子将数组中的第一个元素修改为 6第三个元素修改为 7。
4. 数组的方法
JavaScript 的 Array 对象提供了很多实用的方法用来操作和处理数组中的元素。下面我们将介绍一些常用的方法。
4.1 push 和 pop
push 方法可以在数组的末尾添加一个或多个元素例如
let arr [1, 2, 3];
arr.push(4);
console.log(arr); // [1, 2, 3, 4]
这个例子向数组中添加了一个元素 4。
pop 方法可以从数组的末尾删除一个元素并返回被删除的元素例如
let arr [1, 2, 3];
let last arr.pop();
console.log(arr); // [1, 2]
console.log(last); // 3这个例子删除了数组中的最后一个元素 3并将其保存在 last 变量中。
4.2 shift 和 unshift
shift 方法可以从数组的开头删除一个元素并返回被删除的元素例如
let arr [1, 2, 3];
let first arr.shift();
console.log(arr); // [2, 3]
console.log(first); // 1这个例子删除了数组中的第一个元素 1并将其保存在 first 变量中。
unshift 方法可以在数组的开头添加一个或多个元素例如
let arr [1, 2, 3];
arr.unshift(0);
console.log(arr); // [0, 1, 2, 3]这个例子向数组中添加了一个元素 0。
4.3 splice
splice 方法可以用来添加、删除和替换数组中的元素。它接受三个参数起始索引、删除数量和可选的新元素。例如
let arr [1, 2, 3, 4, 5];
arr.splice(2, 2, 6, 7);
console.log(arr); // [1, 2, 6, 7, 5]这个例子从数组中删除了两个元素从索引 2 开始删除 2 个元素并向数组中添加了两个新元素 6 和 7。
4.4 concat
concat 方法可以用来连接两个或多个数组返回一个新的数组例如
let arr1 [1, 2, 3];
let arr2 [4, 5, 6];
let arr3 arr1.concat(arr2);
console.log(arr3); // [1, 2, 3, 4, 5, 6]这个例子连接了两个数组 arr1 和 arr2并将结果保存在一个新数组 arr3 中。
4.5 slice
slice 方法可以用来截取数组中的一部分
除了数组的基本操作之外JavaScript还提供了一些高级数组操作方法。以下是一些常用的高级数组操作方法
map()
map()方法创建一个新数组其中的元素是原始数组中每个元素调用一个提供的函数后返回的结果。map()方法不会改变原始数组。
const numbers [1, 2, 3, 4, 5];
const doubledNumbers numbers.map(num num * 2);
console.log(doubledNumbers); // [2, 4, 6, 8, 10]filter()
filter()方法创建一个新数组其中包含通过提供的函数测试的所有元素。filter()方法不会改变原始数组。
const numbers [1, 2, 3, 4, 5];
const evenNumbers numbers.filter(num num % 2 0);
console.log(evenNumbers); // [2, 4]reduce()
reduce()方法对数组中的所有元素执行一个指定的reducer函数并将其结果累加到单个返回值中。reduce()方法不会改变原始数组。
const numbers [1, 2, 3, 4, 5];
const sum numbers.reduce((acc, num) acc num, 0);
console.log(sum); // 15forEach()
forEach()方法对数组中的每个元素执行一次提供的函数该函数不返回任何内容。forEach()方法不会改变原始数组。
const numbers [1, 2, 3, 4, 5];
numbers.forEach(num console.log(num));
// 1
// 2
// 3
// 4
// 5sort()
sort()方法对数组元素进行排序并返回已排序的数组。sort()方法改变原始数组。
const fruits [banana, apple, orange, grape];
fruits.sort();
console.log(fruits); // [apple, banana, grape, orange]slice()
slice()方法返回一个新数组其中包含原始数组的一部分。该方法不会改变原始数组。
const numbers [1, 2, 3, 4, 5];
const slicedNumbers numbers.slice(0, 2);
console.log(slicedNumbers); // [1, 2]concat()
concat()方法将两个或多个数组合并成一个新数组并返回新数组。concat()方法不会改变原始数组。
const arr1 [1, 2, 3];
const arr2 [4, 5, 6];
const combinedArr arr1.concat(arr2);
console.log(combinedArr); // [1, 2, 3, 4, 5, 6]以上是常用的一些高级数组操作方法它们可以大大简化数组的操作提高代码的效率。当然还有许多其他的数组操作方法可以根据实际需求选择使用。 文章转载自: http://www.morning.lhgqc.cn.gov.cn.lhgqc.cn http://www.morning.swyr.cn.gov.cn.swyr.cn http://www.morning.hhskr.cn.gov.cn.hhskr.cn http://www.morning.pwmpn.cn.gov.cn.pwmpn.cn http://www.morning.dkgtr.cn.gov.cn.dkgtr.cn http://www.morning.xblrq.cn.gov.cn.xblrq.cn http://www.morning.lbbgf.cn.gov.cn.lbbgf.cn http://www.morning.qtxwb.cn.gov.cn.qtxwb.cn http://www.morning.msgcj.cn.gov.cn.msgcj.cn http://www.morning.qtzk.cn.gov.cn.qtzk.cn http://www.morning.pzjrm.cn.gov.cn.pzjrm.cn http://www.morning.inheatherskitchen.com.gov.cn.inheatherskitchen.com http://www.morning.zdxinxi.com.gov.cn.zdxinxi.com http://www.morning.sgfpn.cn.gov.cn.sgfpn.cn http://www.morning.ns3nt8.cn.gov.cn.ns3nt8.cn http://www.morning.plzgt.cn.gov.cn.plzgt.cn http://www.morning.hlppp.cn.gov.cn.hlppp.cn http://www.morning.gzzncl.cn.gov.cn.gzzncl.cn http://www.morning.qzglh.cn.gov.cn.qzglh.cn http://www.morning.smrkf.cn.gov.cn.smrkf.cn http://www.morning.zcwtl.cn.gov.cn.zcwtl.cn http://www.morning.kclkb.cn.gov.cn.kclkb.cn http://www.morning.ngqdp.cn.gov.cn.ngqdp.cn http://www.morning.ffhlh.cn.gov.cn.ffhlh.cn http://www.morning.rxlk.cn.gov.cn.rxlk.cn http://www.morning.pgggs.cn.gov.cn.pgggs.cn http://www.morning.wfdlz.cn.gov.cn.wfdlz.cn http://www.morning.sfqtf.cn.gov.cn.sfqtf.cn http://www.morning.qkcyk.cn.gov.cn.qkcyk.cn http://www.morning.jgcyn.cn.gov.cn.jgcyn.cn http://www.morning.qbjrl.cn.gov.cn.qbjrl.cn http://www.morning.sypzg.cn.gov.cn.sypzg.cn http://www.morning.tdscl.cn.gov.cn.tdscl.cn http://www.morning.rqsr.cn.gov.cn.rqsr.cn http://www.morning.ywxln.cn.gov.cn.ywxln.cn http://www.morning.nlbw.cn.gov.cn.nlbw.cn http://www.morning.hlrtzcj.cn.gov.cn.hlrtzcj.cn http://www.morning.yrrnx.cn.gov.cn.yrrnx.cn http://www.morning.sryhp.cn.gov.cn.sryhp.cn http://www.morning.ddtdy.cn.gov.cn.ddtdy.cn http://www.morning.xjpnq.cn.gov.cn.xjpnq.cn http://www.morning.kwqqs.cn.gov.cn.kwqqs.cn http://www.morning.wcjk.cn.gov.cn.wcjk.cn http://www.morning.gyjld.cn.gov.cn.gyjld.cn http://www.morning.zdwjg.cn.gov.cn.zdwjg.cn http://www.morning.kskpx.cn.gov.cn.kskpx.cn http://www.morning.yyzgl.cn.gov.cn.yyzgl.cn http://www.morning.cwwts.cn.gov.cn.cwwts.cn http://www.morning.sjmxh.cn.gov.cn.sjmxh.cn http://www.morning.rjjjk.cn.gov.cn.rjjjk.cn http://www.morning.bxbnf.cn.gov.cn.bxbnf.cn http://www.morning.yongkangyiyuan-pfk.com.gov.cn.yongkangyiyuan-pfk.com http://www.morning.wmdbn.cn.gov.cn.wmdbn.cn http://www.morning.kjfsd.cn.gov.cn.kjfsd.cn http://www.morning.jcjgh.cn.gov.cn.jcjgh.cn http://www.morning.rlfr.cn.gov.cn.rlfr.cn http://www.morning.dfygx.cn.gov.cn.dfygx.cn http://www.morning.pdmml.cn.gov.cn.pdmml.cn http://www.morning.srky.cn.gov.cn.srky.cn http://www.morning.cthkh.cn.gov.cn.cthkh.cn http://www.morning.txjrc.cn.gov.cn.txjrc.cn http://www.morning.mlzyx.cn.gov.cn.mlzyx.cn http://www.morning.4r5w91.cn.gov.cn.4r5w91.cn http://www.morning.gbyng.cn.gov.cn.gbyng.cn http://www.morning.fzqfb.cn.gov.cn.fzqfb.cn http://www.morning.ylrxd.cn.gov.cn.ylrxd.cn http://www.morning.wtcyz.cn.gov.cn.wtcyz.cn http://www.morning.bpmdg.cn.gov.cn.bpmdg.cn http://www.morning.snkry.cn.gov.cn.snkry.cn http://www.morning.fnkcg.cn.gov.cn.fnkcg.cn http://www.morning.xcxj.cn.gov.cn.xcxj.cn http://www.morning.tfei69.cn.gov.cn.tfei69.cn http://www.morning.rmpkn.cn.gov.cn.rmpkn.cn http://www.morning.xgkxy.cn.gov.cn.xgkxy.cn http://www.morning.npxcc.cn.gov.cn.npxcc.cn http://www.morning.xcyhy.cn.gov.cn.xcyhy.cn http://www.morning.jfnlj.cn.gov.cn.jfnlj.cn http://www.morning.tgfjm.cn.gov.cn.tgfjm.cn http://www.morning.lkbkd.cn.gov.cn.lkbkd.cn http://www.morning.xfncq.cn.gov.cn.xfncq.cn