在软件开发的过程中JavaScript的编程在所难免,其中对数组的操作尤为常见,这里介绍一下和JavaScript数组相关的某些操作:

1、删除并返回数组的第一个元素——shift方法:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>删除并返回数组元素的第一项</title><script type="text/javascript">var nameArray = ["陈洪涛","李小宁"];console.log(nameArray.shift());for(var nameIndex in nameArray){console.log(nameArray[nameIndex]);}</script></head><body></body>
</html>

说明:第一条输出语句只输出了“陈洪涛”;for循环只输出了一条数据——“李小宁”;

2、删除并返回数组的最后一个元素——pop方法:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title><span style="font-family: Verdana, Arial, 宋体; line-height: 18px; background-color: rgb(249, 249, 249);">删除并返回数组的最后一个元素</span></title><script type="text/javascript">var nameArray = ["陈洪涛","李小宁"];console.log(nameArray.pop());for(var nameIndex in nameArray){console.log(nameArray[nameIndex]);}</script></head><body></body>
</html>

说明:第一条输出语句只输出了“李小宁”;for循环只输出了一条数据——“陈洪涛”;

3、删除数组中某些元素——splice方法:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>删除并返回数组元素的第一项</title><script type="text/javascript">var nameArray = ["陈洪涛","李小宁","吴思娣"];nameArray.splice(1,2);//从第二个元素开始,删除2个元素for(var nameIndex in nameArray){console.log(nameArray[nameIndex]);}</script></head><body></body>
</html>

说明:for循环只输出了“陈洪涛”;

特殊说明:上面splice方法还有一个作用——删除元素并在原删除元素的位置添加新的元素:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>删除数组中某些元素</title><script type="text/javascript">var nameArray = ["陈洪涛","李小宁","吴思娣","郭华山"];nameArray.splice(1,2, "胡楠");//从第二个元素开始,删除2个元素,并添加新的元素for(var nameIndex in nameArray){console.log(nameArray[nameIndex]);}</script></head><body></body>
</html>

说明:for循环依次输出:“陈洪涛”  “胡楠”  “郭华山”;

4、获取已有的数组选定的元素——slice方法:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>获取已有的数组选定的元素</title><script type="text/javascript">var nameArray = ["陈洪涛","李小宁","吴思娣","郭华山"];var newNameArray = nameArray.slice(1,3);//获取已有数组中第二个元素(包括)到第四个元素(不包括)之间的元素for(var newNameIndex in newNameArray){console.log(newNameArray[newNameIndex]);}</script></head><body></body>
</html>

说明:for循环依次输出:“ 李小宁”  “ 吴思娣”;

5、向数组的末尾添加一个或多个元素并返回新的长度——push方法:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>向数组的末尾添加一个或多个元素并返回新的长度</title><script type="text/javascript">var nameArray = ["陈洪涛"];var lenght = nameArray.push("李小宁","吴思娣");console.log(lenght);for(var nameIndex in nameArray){console.log(nameArray[nameIndex]);}</script></head><body></body>
</html>

说明:第一条输出语句输出了“3”;for循环依次输出:“陈洪涛” “李小宁” “吴思娣”;

6、将一个或多个数组合并返回合并后的结果——concat方法

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>将一个或多个数组合并返回合并后的结果</title><script type="text/javascript">var nameArray1 = ["陈洪涛"];var nameArray2 = ["李小宁","吴思娣"];var nameArray = nameArray1.concat(nameArray2);for(var nameIndex in nameArray){console.log(nameArray[nameIndex]);}</script></head><body></body>
</html>

说明:for循环依次输出:“陈洪涛” “李小宁” “吴思娣”;

JavaScript数组的某些操作(一)相关推荐

  1. javascript数组的各种操作

    用 js有非常久了,但都没有深究过js的数组形式.偶尔用用也就是简单的string.split(char).这段时间做的一个项目,用到数组的地方非常多,自以为js高手的自己竟然无从下手,一下狠心,我学 ...

  2. Javascript数组操作(转)

    1.数组的创建 var arrayObj = new Array(); //创建一个数组 var arrayObj = new Array([size]); //创建一个数组并指定长度,注意不是上限, ...

  3. Javascript数组操作

    使用JS也算有段时日,然对于数组的使用,总局限于很初级水平,且每每使用总要查下API,或者写个小Demo测试下才算放心,一来二去,浪费不少时间:思虑下,堪能如此继续之?当狠心深学下方是正道. 原文链接 ...

  4. javaScript数组操作--有道笔记整理

    javascript之数组操作 1.数组的创建 var arrayObj = new Array(); //创建一个数组 var arrayObj = new Array([size]); //创建一 ...

  5. JavaScript数组操作 [Z]

    函数join(delimiter): 把数组中各个元素使用分隔符(delimiter)拼成字符串 concat(array1, array2, ...): 合并数组,对原数组不影响,只是返回合并后的新 ...

  6. JavaScript 数组操作大全

    1. pop() pop() 方法从数组中删除最后一个元素: 实例 var fruits = ["Banana", "Orange", "Apple& ...

  7. [JavaScript] JavaScript数组挖掘,不只是讲数组哟(2)

    课程来源:后盾人 上一篇的内容:[JavaScript] JavaScript数组挖掘,不只是讲数组哟 数组引用类型分析,多维数组,用Array.of为数组创建细节,类型检测与转换,在一个数组后面加一 ...

  8. 如何将JavaScript数组信息导出到csv(在客户端)?

    本文翻译自:How to export JavaScript array info to csv (on client side)? I know there are lot of questions ...

  9. 创建零填充JavaScript数组的最有效方法?

    在JavaScript中创建任意长度的零填充数组的最有效方法是什么? #1楼 使用对象符号 var x = []; 零填充? 喜欢... var x = [0,0,0,0,0,0]; 充满" ...

  10. splice方法_[7000字]JavaScript数组所有方法基础总结

    基础决定一个人的上限,很多时候我们感叹别人在实现一个功能时使用方法的精妙,并且反思,为什么别人想的出来自己却想不出来?我觉得主要是因为对于基础的掌握上有很大的差距.本文总结数组的所有方法的基础使用,希 ...

最新文章

  1. pta两个有序链表的合并_21. 合并两个有序链表
  2. [转载]使用C#的BitmapData
  3. [密码学] 离散对数比特安全性
  4. js 中导出excel 较长数字串会变成科学计数法
  5. [推荐]经典炸弹人手机游戏[年兽防御战2]
  6. 再有人问你volatile是什么,把这篇文章也发给他(深入分析)
  7. map key char*
  8. Linux如何生成列表
  9. 真神器!在家也能控制公司的电脑了
  10. 初学Jmeter的摘抄学习总结----------基础知识篇
  11. hibernate 入门案例
  12. 如何有效阅读英文数据手册?
  13. SpringBoot-Google二步验证
  14. 服务器 显示w3wp.exe,w3wp.exe占用cpu过高的解决方法
  15. 卡方(χ2),四格表应用条件,理论频数
  16. 人体手脚部位与内脏的对应关系图
  17. Dell电脑插入耳机不能识别
  18. 双塔模型-语义索引策略 [In-batch Negatives]
  19. 四类九种移位寄存器总结(循环(左、右、双向)移位寄存器、逻辑和算术移位寄存器、串并转换移位寄存器、线性反馈移位寄存器LFSR|verilog代码|Testbench|仿真结果)
  20. 推荐系统中的排序学习

热门文章

  1. android Xmpp+openfire 消息推送 :SASL authentication failed using mechanism DIGEST-MD5
  2. 姆巴佩独造三球一战成名 阿里云打破世界杯流量纪录 1
  3. 【vijos】P1190 繁忙的都市
  4. 从零单排PAT1015,1016,1017,1018
  5. apache SSL配置
  6. 1622: [Usaco2008 Open]Word Power 名字的能量
  7. 我为什么不喜欢网赚和SEO
  8. 创业者请不断自问:我能帮用户解决什么问题?
  9. char,varchar,nvarchar以及datetime和smalldatetime的区别
  10. 17. Contoller(2)