欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 前端技术 > javascript >内容正文

javascript

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

发布时间:2025/3/11 javascript 24 豆豆
生活随笔 收集整理的这篇文章主要介绍了 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 lastfruits.push("orange");document.write("<br />")fruits.forEach(function(item,index,array){document.write("Fruits["+index+"] = "+item+"<br />");});//Insert Item at Frontfruits.unshift("cherry");document.write("<br />")fruits.forEach(function(item,index,array){document.write("Fruits["+index+"] = "+item+"<br />");});//Remove Item from Lastfruits.pop();document.write("<br />")fruits.forEach(function(item,index,array){document.write("Fruits["+index+"] = "+item+"<br />");});//Remove Item from Frontfruits.shift();document.write("<br />")fruits.forEach(function(item,index,array){document.write("Fruits["+index+"] = "+item+"<br />");});//Finding Index of Itemvar index = fruits.indexOf("banana");document.write("<br />");document.write("Index of banana : "+index+"<br />");//Removing any item from Listdocument.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 : appleFruits[0] = apple Fruits[1] = mango Fruits[2] = banana Fruits[3] = grapes Fruits[4] = guavaFruits[0] = apple Fruits[1] = mango Fruits[2] = banana Fruits[3] = grapes Fruits[4] = guava Fruits[5] = orangeFruits[0] = cherry Fruits[1] = apple Fruits[2] = mango Fruits[3] = banana Fruits[4] = grapes Fruits[5] = guava Fruits[6] = orangeFruits[0] = cherry Fruits[1] = apple Fruits[2] = mango Fruits[3] = banana Fruits[4] = grapes Fruits[5] = guavaFruits[0] = apple Fruits[1] = mango Fruits[2] = banana Fruits[3] = grapes Fruits[4] = guavaIndex of banana : 2Fruits[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 | 数组的常用属性和方法的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。