JavaScript | 数组的常用属性和方法

JavaScript的通用属性和数组方法 (Common properties and methods of array in JavaScript )

Properties/MethodsDescriptions
array.lengthReturns 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

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

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

相关文章

php dbutils 使用,dbutilsapi

相对lisp?而?言,可以使?用.net的强?大api,实现各种酷炫功能。相对c#及arx?...文件utils.py的模块名分别是mycompany.utils和 mycompany.web.utils。 mycompany......CouchDB -b 关闭后台运行的 CouchDB : CouchDB -d web 访问:http://127.0.0.1:5984/_utils/index.html E-…

html 导航栏

<!DOCTYPE html> <html> <head> <meta charset"utf-8"> <title>lvnian学习(http://lvnian.blog.51cto.com/)</title> <style> ul {list-style-type:none;margin:0;padding:0; }a:link,a:visited{display:block;font-weigh…

将搜索二叉树转换为链表_将给定的二叉树转换为双链表(DLL)

将搜索二叉树转换为链表Given a Binary tree and we have to convert it to a Doubly Linked List (DLL). 给定二叉树&#xff0c;我们必须将其转换为双链表(DLL)。 Algorithm: 算法&#xff1a; To solve the problem we can follow this algorithm: 为了解决这个问题&#…

cuda编程_CUDA刷新器:CUDA编程模型

CUDA刷新器&#xff1a;CUDA编程模型 CUDA Refresher: The CUDA Programming Model CUDA&#xff0c;CUDA刷新器&#xff0c;并行编程 这是CUDA更新系列的第四篇文章&#xff0c;它的目标是刷新CUDA中的关键概念、工具和初级或中级开发人员的优化。 CUDA编程模型提供了GPU体系结…

php curl_error源码,PHP curl_error函数

PHP curl_error函数(PHP 4 > 4.0.3, PHP 5)curl_error — 返回一个保护当前会话最近一次错误的字符串说明string curl_error ( resource $ch )返回一条最近一次cURL操作明确的文本的错误信息。参数ch由 curl_init() 返回的 cURL 句柄。返回值返回错误信息或 (空字符串) 如果…

SQL中Where与Having的区别

“Where” 是一个约束声明&#xff0c;使用Where来约束来之数据库的数据&#xff0c;Where是在结果返回之前起作用的&#xff0c;且Where中不能使用聚合函数。“Having”是一个过滤声明&#xff0c;是在查询返回结果集以后对查询结果进行的过滤操作&#xff0c;在Having中可以使…

java 逻辑表达式 布尔_使用基本逻辑门实现布尔表达式

java 逻辑表达式 布尔将布尔表达式转换为逻辑电路 (Converting Boolean Expression to Logic Circuit) The simplest way to convert a Boolean expression into a logical circuit is to follow the reverse approach in which we start from the output of the Boolean expre…

python自然语言处理书籍_精通Python自然语言处理pdf

自然语言处理&#xff08;NLP&#xff09;是有关计算语言学与人工智能的研究领域之一。NLP主要关注人机交互&#xff0c;它提供了计算机和人类之间的无缝交互&#xff0c;使得计算机在机器学习的帮助下理解人类语言。 本书详细介绍如何使用Python执行各种自然语言处理&#xff…

通达oa 2013 php解密,通达OA漏洞学习 - 安全先师的个人空间 - OSCHINA - 中文开源技术交流社区...

说明通达OA漏洞在去年上半年已爆出&#xff0c;这不趁着周末没事做&#xff0c;将源码下载下来进行复现学习。文件包含测试文件包含检测&#xff0c;payload1:ip/ispirit/interface/gateway.php?json{"url":"/general/../../mysql5/my.ini"}利用文件包含访…

温赵轮 访谈

“温赵轮”三大软狗&#xff0c;你听说过吗&#xff1f;今天的1024访谈录给大家介绍的就是程序员中当之无愧的偶像组合——温赵轮。 Winter寒冬。阿里P8&#xff0c;正在向P9的道路上奔跑。传说中的他有钱、出身好&#xff0c;可不是搞互联网的屌丝程序员。 老赵&#xff0c;…

linux开源文档管理系统_Linux中的系统管理员问题 免费和开源软件

linux开源文档管理系统根帐号 (Root Account) The "root" account is the most unrestrictive account on a Linux Operating system. This account enables you to complete all features of System admin, including accounts, changing client passwords, looking…

matlab上机实验1,上机实验1:熟悉matlab基本操作

其中 x 在 [-2, 2] 间共等切分为 21 点&#xff0c;y 在 [-1, 1] 间共等切分为 21 点&#xff0c;所以此曲面共有 21*21441 个点。a. 请用预设的颜色对应表(Colormap)来画出此曲面。 b. 请以曲面的斜率来设定曲面的颜色。 c. 请以曲面的曲率来设定曲面的颜色。2. 请用 meshc 指…

公众号 -「前端攻略 开光篇」

作为一枚程序员&#xff0c;每件重要项目的开始都忍不住使用"Hello World"。 这个公众号是不是来晚了&#xff1f;如果你有这个疑问&#xff0c;那么我想说&#xff1a;对于写作和思考&#xff0c;任何时候都不晚。我用四个简单的自问自答&#xff0c;来讲讲这个前端…

python 桌面应用 启动缓慢_如何加快Python 应用的启动时间

我听说pipenv9.0.2已经发布&#xff0c;启动时间有了很大的改进。 我很快就试了一下&#xff0c;但我觉得并不快。所以我用Python3.7的新特性来研究它。 在本文中&#xff0c;我将介绍该特性以及如何使用它。 启动时间≒导入时间 例如&#xff0c;pipenv -h 的执行时间比显示帮…

python单词首字母大写_在Python中将每个单词的首字母大写

python单词首字母大写Here, we are implementing a python program to capitalizes the first letter of each word in a string. 在这里&#xff0c;我们正在实现一个python程序来大写字符串中每个单词的首字母。 Example: 例&#xff1a; Input: "HELLO WORLD!"O…

matlab中求模最大,matlab求取模极大值时出错

本帖最后由 Nate_ 于 2016-4-17 15:57 编辑points1024 时&#xff0c;有波形输出&#xff0c;但信号有5438个点。改为5438就不行。主程序&#xff1a;%小波模极大值重构是采用的交替投影法close all;points5438; level4; sr360; num_inter6; wfdb4;%所处理数据的…

stl向量_如何检查C ++ STL中向量中是否存在元素?

stl向量Given a vector and an element to be searched in the vector. 给定一个向量和要在向量中搜索的元素。 To check whether an elements exists in a vector or not – we use find() function. find() function takes 3 arguments. 要检查向量中是否存在元素 –我们使用…

java socket如何请求485协议_javaSE第十五部分 网络编程(1)Socket和ServerSocket

网络编程基础知识C/S结构&#xff1a;全称为Client/Server结构&#xff0c;是指客户端和服务器结构。常见程序有&#xff31;&#xff31;、迅雷等软件。B/S结构&#xff1a;全称为Browser/Server结构&#xff0c;是指浏览器和服务器结构。常见浏览器有谷歌、火狐等。两种架构各…

【分享】linux下u盘使用

2019独角兽企业重金招聘Python工程师标准>>> linux下u盘使用 方案一&#xff1a; Linux不像Windows一样&#xff0c;接上新硬件后可以自动识别&#xff0c;在Linux下无法自动识别新硬件的&#xff0c;需要手动去识别。USB移动存储设备通常被识别为sda1&#xff0c;…

kotlin中判断字符串_Kotlin程序删除字符串中所有出现的字符

kotlin中判断字符串Given a string and a character, we have to remove all occurrences of the character in given string. 给定一个字符串和一个字符&#xff0c;我们必须删除给定字符串中所有出现的字符。 Example: 例&#xff1a; Input:string "includeHelp Del…