JavaScript | 数组的常用属性和方法
JavaScript的通用属性和数组方法 (Common properties and methods of array in JavaScript )
Properties/Methods | Descriptions |
---|---|
array.length | Returns the length of the array/total number of elements of the array |
array[index] | Returns the item name stored at “index” position |
array.push(item) | Inserts item at the end of the array |
array.unshift(item) | Inserts item at the front of the array |
array.pop() | Removes item from the end of the array |
array.shift() | Removes item from the end of the array |
array.indexOf(item) | Returns the index of the item |
array.splice(index,1)) | Removes item from the list (index must be provided) |
属性/方法 | 内容描述 |
---|---|
array.length | 返回数组的长度/数组中元素的总数 |
数组[索引] | 返回存储在“索引”位置的项目名称 |
array.push(item) | 在数组末尾插入项目 |
array.unshift(项目) | 在数组的前面插入项目 |
array.pop() | 从数组末尾删除项目 |
array.shift() | 从数组末尾删除项目 |
array.indexOf(item) | 返回项目的索引 |
array.splice(index,1)) | 从列表中删除项目(必须提供索引) |
Code:
码:
<html>
<head>
<script>
var fruits = [
"apple",
"mango",
"banana",
"grapes",
"guava"
];
</script>
</head>
<body>
<script>
document.write("<h3>List Of Array Items</h3>")
document.write("<hr />");
document.write("<ul>");
for(var i=0;i<fruits.length;i++){document.write("<li>"+fruits[i]+"</li>");
}
document.write("</ul>");
document.write("<hr />")
var size=fruits.length;
document.write("Size of Array : "+size+"<br />");
var first = fruits[0];
document.write("First Item of Array : "+first+"<br /><br />");
fruits.forEach(function(item,index,array){document.write("Fruits["+index+"] = "+item+"<br />");
});
//Insert Item at last
fruits.push("orange");
document.write("<br />")
fruits.forEach(function(item,index,array){document.write("Fruits["+index+"] = "+item+"<br />");
});
//Insert Item at Front
fruits.unshift("cherry");
document.write("<br />")
fruits.forEach(function(item,index,array){document.write("Fruits["+index+"] = "+item+"<br />");
});
//Remove Item from Last
fruits.pop();
document.write("<br />")
fruits.forEach(function(item,index,array){document.write("Fruits["+index+"] = "+item+"<br />");
});
//Remove Item from Front
fruits.shift();
document.write("<br />")
fruits.forEach(function(item,index,array){document.write("Fruits["+index+"] = "+item+"<br />");
});
//Finding Index of Item
var index = fruits.indexOf("banana");
document.write("<br />");
document.write("Index of banana : "+index+"<br />");
//Removing any item from List
document.write("<br />");
var pos = fruits.indexOf("banana");
fruits.splice(pos,1)
fruits.forEach(function(item,index,array){document.write("Fruits["+index+"] = "+item+"<br />");
});
</script>
</body>
</html>
Output
输出量
List Of Array Items
apple
mango
banana
grapes
guava
Size of Array : 5
First Item of Array : apple
Fruits[0] = apple
Fruits[1] = mango
Fruits[2] = banana
Fruits[3] = grapes
Fruits[4] = guava
Fruits[0] = apple
Fruits[1] = mango
Fruits[2] = banana
Fruits[3] = grapes
Fruits[4] = guava
Fruits[5] = orange
Fruits[0] = cherry
Fruits[1] = apple
Fruits[2] = mango
Fruits[3] = banana
Fruits[4] = grapes
Fruits[5] = guava
Fruits[6] = orange
Fruits[0] = cherry
Fruits[1] = apple
Fruits[2] = mango
Fruits[3] = banana
Fruits[4] = grapes
Fruits[5] = guava
Fruits[0] = apple
Fruits[1] = mango
Fruits[2] = banana
Fruits[3] = grapes
Fruits[4] = guava
Index of banana : 2
Fruits[0] = apple
Fruits[1] = mango
Fruits[2] = grapes
Fruits[3] = guava
翻译自: https://www.includehelp.com/code-snippets/common-properties-and-methods-of-array-in-javascript.aspx
JavaScript | 数组的常用属性和方法相关推荐
- JavaScript之DOM常用属性及方法详解
一.什么是DOM? DOM:文档对象模型(Document Object Model,简称 DOM),是 W3C 组织推荐的处理可扩展标记语言(HTML或者XML)的标准编程接口. W3C 已经定义了 ...
- Node.js process 模块常用属性和方法
Node.js是常用的Javascript运行环境,本文和大家发分享的主要是Node.js中process 模块的常用属性和方法,希望通过本文的分享,对大家学习Node.js http://www.m ...
- Ajax--概述、xhr对象的常用属性和方法、xhr的常用事件、xhr对象发送POST请求、xhr对象发送GET请求、xhr对象的兼容性问题、数据交换格式(XML、JSON)
一.概述 1.1 发展历程 在开始之前先来看一下Ajax的工作原理吧,如下图所示: Ajax全称Asynchronous javascript and xml(异步 JavaScript 和 XML) ...
- JavaScript 数组遍历的五种方法(转)
转自:JavaScript 数组遍历的五种方法 这篇文章主要介绍了JavaScript 数组遍历的五种方法,帮助大家更好的理解和学习使用JavaScript,感兴趣的朋友可以了解下 在使用 JavaS ...
- vb中WindowsMediaPlayer的常用属性和方法
vb中WindowsMediaPlayer的常用属性和方法 enableContextMenu:Boolean 显示/不显示播放位置的右键菜单 fullScreen:boolean 全屏显示 stre ...
- SVG DOM常用属性和方法介绍
将以Adobe SVG Viewer提供的属性和方法为准,因为不同解析器对JavaScript以及相关的属性和方法支持的程度不同,有些方法和属性是某个解析器所特有的.SVG支持DOM2标准. 12.2 ...
- JavaScript数组去重的五种方法
JavaScript数组去重的五种方法 先简单准备一个数组,用于方法的实验: let array = [1,1,2,3,4,4,1,5,6,6,7,7,7]; console.log(`去重前的数组: ...
- ExtJs 备忘录(9)—— Ext常用属性、方法小结 [系列完]
一.Ext 1.1 Ext.isEmpty(v, allowBlank) //是否为空[链接] 1.2 Ext.isArray(v) //是否为数组集合 1.3 Ext.isPrimitive(v) ...
- selenium提取数据之driver对象的常用属性和方法
selenium提取数据之driver对象的常用属性和方法 在使用selenium过程中,实例化driver对象后,driver对象有一些常用的属性和方法 driver.page_source 当前标 ...
最新文章
- 【FFmpeg】函数详解(三)
- LeetCode刷题-4
- 百度Apollo:CTO级无人车大牛不稀缺,我们这也就百八十个吧
- MIT_18.03_微分方程_Laplace_Transform_拉普拉斯变换_Notes
- filter与servlet对照
- 十二周二次课 12.6 Nginx安装 12.7 默认虚拟主机 12.8 Nginx用户认证 12.
- 商务办公软件应用与实践【1】
- 团队管理33-典型场景(向下沟通)
- Openjudge:苹果和虫子
- 优秀程序猿写技术文档的正确姿势
- 怎么查看qq登陆记录
- 图表分析2020年和2018年北京积分落户数据
- 仿真软件proteus构建LCD1602显示字符串实验
- 区块链革命 - 第2篇 转型 - 第4章 重新设计公司架构:核心与边缘
- 数据结构第七次上机实验-解题报告
- python分治算法_Python算法:分治法
- 基于瞬时功率理论的APF的SIMULINK模型仿真
- Windows下载安装docker详细步骤
- 【转】八个基本的 Docker 容器管理命令
- unity怎么在文本中修改字体_(转载)Unity3D开发之编辑器统一修改Text字体
热门文章
- PHP递归删除目录面试题,PHP 递归删除目录中文件
- java类中声明log对象_用于Android环境,java环境的log打印,可打印任何类型数据
- android html转pdf工具,android – 使用iText库将html转换为pdf时未应用hr的内联CSS
- 控制附件的大小 php,wordpress如何修改默认上传附件限制大小
- 一秒执行一次_《一秒钟》:一贯的粗旷式抓大放小,张艺谋的自命题作业总是要观众自己再做一遍...
- linux怎么添加头文件目录下,linux下编写c++,include的那些头文件在什么地方?
- python垃圾回收机制为什么标记能解决循环引用问题_python 关于循环引用以及标记清除的问题...
- Iptables防火墙原理
- 集群(cluster)amp;高可用性(HA)概念
- Grafana文档(在Centos / Redhat上安装)