日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

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

發布時間:2025/3/11 javascript 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 | 数组的常用属性和方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。