在软件开发的过程中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 数组的常用操作

    JavaScript splice 方法 splice 方法用于插入.删除或替换数组的元素.语法如下: array_object.splice(start, num, element1, elemen ...

  2. javascript 数组的常用操作函数

    join() Array.join(/* optional */ separator) 将数组转换为字符串,可带一个参数 separator (分隔符,默认为“,”). 与之相反的一个方法是:Stri ...

  3. JavaScript数组的某些操作(二)

    7.颠倒数组中元素的顺序(注意:不是为数组排序)--reverse方法 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitiona ...

  4. Javascript 数组的一些操作

    (1) shift  删除原数组第一项,并返回删除元素的值:如果数组为空则返回undefined var a = [1,2,3,4,5]; var b = a.shift(); //a:[2,3,4, ...

  5. Javascript数组操作

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

  6. Javascript数组操作(转)

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

  7. JavaScript数组常用操作

    前言 相信大家都用惯了jquery或者underscore等这些类库中常用的数组相关的操作,如$.isArray,_.some,_.find等等方法.这里无非是对原生js的数组操作多了一些包装. 这里 ...

  8. RX学习笔记:JavaScript数组操作

    RX学习笔记:JavaScript数组操作 2016-07-03 增删元素 unshift() 在数组开关添加元素 array.unshift("value"); array.un ...

  9. javascript数组操作汇总

    javascript之数组操作 - 不悔的青春 - 博客园 1.数组的创建 var arrayObj = new Array(); //创建一个数组 var arrayObj = new Array( ...

随机推荐

  1. tomact和eclipse的关联

    tomact和eclipse的关联有很多文档,这里说下下面的问题: 问题: tomact安装成功,点击startup.sh能正常访问,通过eclipse启动后,不能打开8080页面  解决: l  重 ...

  2. Spring-IOC源码解读3-依赖注入

    当容器已经载入了BeanDefinition的信息完成了初始化,我们继续分析依赖注入的原理,需要注意的是依赖注入是用户第一次向IOC容器获取Bean的时候发生的,这里有个例外,那就是如果用户在Bean ...

  3. poj 1269 直线间的关系

    Intersecting Lines Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9360   Accepted: 421 ...

  4. *AtCoder Regular Contest 096E - Everything on It

    $n \leq 3000$个酱,丢进拉面里,需要没两碗面的酱一样,并且每个酱至少出现两次,面的数量随意.问方案数.对一给定质数取模. 没法dp就大力容斥辣.. $Ans=\sum_{i=0}^n (- ...

  5. 在GridView中的每一页末尾添加空行

    原文发布时间为:2008-08-03 -- 来源于本人的百度文章 [由搬家工具导入] protected void GridView1_RowCreated(object sender, GridVi ...

  6. Codeforces 123 E Maze

    Discription A maze is represented by a tree (an undirected graph, where exactly one way exists betwe ...

  7. 单点登录CAS-Demo

    版权声明:本文为博主原创文章,未经博主允许不得转载.   目录(?)[-] 1安全证书配置 2部署服务端CAS-Server 3部署CAS-Client 4测试SSO   1,安全证书配置 CAS默认 ...

  8. BUPT复试专题—排序(2009)

    题目描述 查找序列a 中小于 b 的第 i 个数的数的个数 输入 输入有多组,每组四行第一行:序列a个数N第二行:(序列a的)N个数,升序排列第三行:序列b个数M 第四行:(序列b的)M个数,升序排列 ...

  9. weexpack build android 和 weexpack run android 报错 及 解决方案

    1. weexpack build android (1)Configuring > 0/3 projects > root project > Resolving dependen ...

  10. SAP 锁对象 基本概念与基本操作 SE11

      一.SAP为什么要设置锁:     1,保持数据的一致性     假设几个用户要訪问相同的资源,须要找到一种同步訪问的方法去保持数据的一致性.比方说,在航班预订系统中,须要检查还有没有空座位,当检 ...