JavaScript中的数组

Here we are discussing one of the most useful data structure, Array.

在这里,我们讨论最有用的数据结构之一Array

By conventional definition of arrays, "Arrays are the homogeneous collection of data types. But in JS, Arrays simply are the collection of different data types, may or may not be the same.

按照常规的数组定义“数组是数据类型的同类集合。 但是在JS中 ,数组只是不同数据类型的集合,可能相同也可能不同。

Now let’s see an example:

现在让我们看一个例子:

let week = ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturdday','Sunday'];
console.log(week[0]);       //line 1
console.log(week[6]);       //line 2
console.log('Size of array',week.length);   //line 3
week.pop();      //line 4
week.pop();
console.log(week);  //line 5
week.push('Saturday');
// week.push(1)     //Line 6
console.log('After Line 6\n',week);
week.unshift('Sunday');  //Line 7
console.log('After Line 7\n',week);
week.splice(3,1,"IncludeHelp");  //line 8
console.log('After line 8\n',week);

In the above code snippet, we created an array of name week, we created with 7 elements. In line 1 and 2 we used console.log to print the element of our array week.

在上面的代码片段中,我们创建了一个名称为week的数组,并创建了7个元素。 在第1行和第2行中,我们使用console.log打印数组周的元素。

We can access to a particular element of an array by using [ ] proceeded by name of the array and inside the square brackets we write the index of the element. Like arrayName[Index]

我们可以通过使用[]来访问数组的特定元素,在数组的名称后面加上[],然后在方括号内编写元素的索引。 像arrayName [Index]

The interesting thing is indexing of the array starts with 0, not 1, that means the index of Monday in our week array will be 0, not 1 and then the last element will become 6. Thus line 1 & 2 we will get a first and last element of the array respectively.

有趣的是,数组的索引从0开始,而不是1,这意味着我们周数组中的Monday的索引将是0,而不是1,然后最后一个元素将成为6。因此,第1和2行将获得第一个和数组的最后一个元素。

Now move to line 3, week.length as the name suggests it return the length of the array, in our case, it’s 7. We can use it as arrayName.length and it will be an integer value.

现在移至第3行, 如其名称所示, week.length返回数组的长度,在本例中为7。我们可以将其用作arrayName.length ,它将是一个整数值。

The pop function will delete the last element from the array every time being called, the syntax for it is arrayName.pop(). Therefore, in line 5, we got 5 elements after 2 successive pops. Similarly, shift function will delete an element from the beginning and its syntax is arrayName.shift().

pop函数每次被调用时都会从数组中删除最后一个元素 ,其语法为arrayName.pop() 。 因此,在第5行中,连续2次弹出后得到5个元素。 同样,shift函数将从头删除元素,其语法为arrayName.shift() 。

The push function is used to add an element at the end of the array, and its syntax is arrayName.push(element), where the element is data type independent it could be integer floating value or a string. Try uncommenting line 6 and observe what happens. But if we wanted to add an element at the beginning we shall use unshift function which is having a syntax similar to pop as arrayName.unshift(element) like we used in line 7.

push函数用于在数组的末尾添加元素 ,其语法为arrayName.push(element) ,其中元素是独立于数据类型的,它可以是整数浮点值或字符串。 尝试取消注释第6行,并观察会发生什么。 但是,如果我们想在开始时添加一个元素,我们将使用unshift函数,其语法类似于pop作为arrayName.unshift(element),就像在第7行中使用的那样。

Now, move to splice function which basically, removes an element or series of elements from a position and optionally can add/insert an element at that position. Let’s see its syntax, arrayName.splice(startIndex, count,[optional] string), here the start index is the index from where the first element will delete and the count tells the number of elements to delete, if it’s one then only the element at startIndex will be delete and say count is two then the startIndex and element next to it will be deleted. If no third argument passed so they will be deleted but it contains some element they will insert at that index.splice can be used to add an element at a particular index with the following way:

现在,转到拼接功能,该功能基本上是从一个位置删除一个元素或一系列元素,并可以选择在该位置添加/插入元素 。 让我们看看它的语法arrayName.splice(startIndex,count,[可选]字符串) ,这里的起始索引是第一个元素将要删除的索引,而count则指示要删除的元素数,如果是,则仅删除startIndex处的元素将被删除,并说count为2,则startIndex及其旁边的元素将被删除。 如果没有传递第三个参数,那么它们将被删除,但其中包含一些元素,它们将在该索引处插入.splice可通过以下方式用于在特定索引处添加元素:

arrayName.splice(Index,0, element) this will add the element at the mentioned index without deleting the prior elements, their position will be increment by one.

arrayName.splice(Index,0,element)这将在提到的索引处添加元素,而不删除先前的元素,它们的位置将增加一。

Output for the above code:

上面代码的输出:

arrays output in JS

Read mode: forEach method of array

读取方式: 数组的forEach方法

翻译自: https://www.includehelp.com/code-snippets/arrays-in-javascript.aspx

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/377429.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【C++基础】异常处理机制概要

目录C的异常处理方法知识要点踹扔抓的代码块示例两种处理被0除的方法异常处理机制的优点其他语言中的异常处理C函数异常声明C的异常处理方法知识要点 理解“踹扔抓”三部曲的结构,尤其是catch是怎么匹配异常的。 知道C标准库中的异常类都是从exception继承下来的&am…

[转载]Struts2 获得Session和Request

转自http://www.blogjava.net/lyyb2001/archive/2008/03/07/184593.html 在struts1中,获得到系统的request或者session对象非常方便,都是按照形参传递的,但是在struts2中,request和session都被隐藏了struts提供两种方式访问sessio…

WPS根据章节编号依次排序

第Ⅲ章节有四小部分,分别为A、B、C、D 第Ⅳ章节要重新开始编号,从A开始 操作步骤: ①再D后面回车(红线位置回车),生成E ②把Ⅳ中待写内容写到E中 ③将E复制到Ⅳ下,这里需要注意D的换行也要复制…

【C++基础】异常匹配与内建异常类

目录异常匹配catch: 按异常类型匹配为何要使用异常类内建异常类标准库中的异常基类标准库中的异常类例1:vector下标访问越界out_of_range异常例2:内存分配失败bad_alloc异常例3:侧向转换失败bad_cast异常类几种情况,使用对应异常异…

scala 访问修饰符_Scala中的访问修饰符

scala 访问修饰符Access modifiers are used in order to restrict the usage of a member function to a class or a package. Using access modifiers data hiding takes place which is a very important concept of OOPs. 访问修饰符用于将成员函数的使用限制为类或包。 使…

小试---EF5.0入门实例1

现在做个小练习吧~~~ 第一步:首先新建一个数据库名字为Test;数据库里面只有一个表UserTable 脚本为: USE [master] GO /****** 对象: Database [Test] 脚本日期: 12/15/2013 18:51:54 ******/ CREATE DATABASE [Test] ON PRIMARY ( NAME NTest, F…

iScroll4 禁止select等页面元素默认事件的解决方法 转

iScroll4 禁止select等页面元素默认事件的解决方法起因在于onBeforeScrollStart : function(e){ e.preventDefault(); },这一行,iSroll禁止了事件的默认行为,导致select,option,textarea等元素无法点击。解决方法也很简单&#xf…

C++中比较两个浮点数是否相等

来源&#xff1a; https://stackoverflow.com/a/37686/3242645 代码&#xff1a; #include <cmath> #include <limits> bool AreSame(double a, double b) {return std::fabs(a - b) < std::numeric_limits<double>::epsilon(); }

MPEG的完整形式是什么?

MPEG&#xff1a;运动图像专家组 (MPEG: Moving Picture Experts Group) MPEG is an abbreviation of Moving Picture Experts Group. It is a working group of authorities that is founded to establish standards for audio and video compression and transmission. The a…

正则 去除html标记

//string regexstr "<[^>]*>"; //去除所有的标签 //"<script[^>]*?>.*?</script>" //去除所有脚本&#xff0c;中间部分也删除 // string regexstr "<img[^>]*>"; //去除图片的正则 // string regexstr &…

自画PopMenu弹出

BorderColor:TColor; //边框颜色FillColor:TColor; //未选中填充颜色TextColor:TColor; //未选中字体颜色SelectTextColor:TColor; //选中字体颜色SelectFillColor:TColor; //选中填充颜色SideBuffer:Integer; //边框宽度procedure TForm1.FormCreate(Sender: TObject); b…

安利一款倒计时插件---雨滴桌面

内容来自B站(搜索Rainmeter即可)&#xff0c;里面教程很多&#xff0c;因为视频看的有点麻烦&#xff0c;故进行了整理 一、下载安装包、解压、安装 免费下载连接&#xff0c;不需要积分 skin文件夹存放皮肤的一些配置文件&#xff0c;因为原本皮肤太low了 第二个是可执行文…

【C++基础】自定义异常类与多重捕获

目录自定义异常类构建过程例&#xff1a;Vec3D类的数组下标越界的异常类捕获多种无关异常不同的异常的捕获捕获派生异常异常处理的次序例子&#xff1a;多重捕获异常类catch块的参数类型可以不用引用类型吗?自定义异常类 自定义异常类通常由exception或其后代类派生。这样我们…

gprs 睡眠模式_GPRS的完整形式是什么?

gprs 睡眠模式GPRS&#xff1a;通用分组无线业务 (GPRS: General Packet Radio Service) GPRS is an abbreviation of General Packet Radio Service. It is a non-voice, high-level speed packet switching technology planned for GSM networks. On 2G and 3G cellular tran…

int main(int argc,char* argv[])讲解

分类&#xff1a; 学习笔记2011-11-07 21:502354人阅读评论(0)收藏举报dos编译器pathunixcommandc在最近学习中老是遇到 int main(int argc,char* argv[])&#xff0c;以为就是简单的参数应用了&#xff0c;但是看代码是没能理解参数的具体传递过程&#xff0c;上网…

Maven实战(七)——常用Maven插件介绍(上)

我们都知道Maven本质上是一个插件框架&#xff0c;它的核心并不执行任何具体的构建任务&#xff0c;所有这些任务都交给插件来完成&#xff0c;例如编译源代码是由maven-compiler-plugin完成的。进一步说&#xff0c;每个任务对应了一个插件目标&#xff08;goal&#xff09;&a…

【设计模式之美】<Reading Notes>抽象类与接口

抽象类特性 1、抽象类不允许被实例化&#xff0c;只能被继承。 2、抽象类可以包含属性和方法。方法既可以包含代码实现&#xff0c;也可以不包含代码实现。不包含代码实现的方法叫做抽象方法。 3、子类继承抽象类&#xff0c;必须实现抽象类中的所有抽象方法。 接口特性 1、…

多线程之间共享数据的实现

1&#xff1a;如果每个线程执行的代码相同&#xff0c;可以使用同一个Runnable对象&#xff0c;然后将共享的数据放在Runnable里面&#xff0c;来实现数据的共享。 例如买票系统... package com.cn.gbx;import java.util.Date; import java.util.Random; import java.util.Time…

AIX的完整形式是什么?

AIX&#xff1a;高级交互式主管 (AIX: Advanced Interactive Executive) AIX is an abbreviation of "Advanced Interactive Executive". AIX是“ Advanced Interactive Executive”的缩写 。 It is a progression sequence of proprietary UNIX operating systems …

c#生成随机字符串 用做批量申请账号时的随机密码还是相当不错的

//随机字符串生成器的主要功能如下&#xff1a; //1、支持自定义字符串长度 //2、支持自定义是否包含数字 //3、支持自定义是否包含小写字母 //4、支持自定义是否包含大写字母 //5、支持自定义是否包含特殊符号 //6、支持自定义字符…