在软件开发的过程中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. 对拍程序(Win)

    代码如下: @echo off :again rand.exe echo "rand finish" asd.exe echo "1.exe finish" 未 ...

  2. [luoguP4035] [JSOI2008]球形空间产生器(高斯消元)

    传送门 设球心的坐标为未知量 用最后一个点来表示球面到球心的距离,那么它和前n个式子相等 移项乱搞 最后高斯消元 #include <cmath> #include <cstdio& ...

  3. run as maven build时报错

    eclipse中使用maven插件的时候,运行run as maven build的时候报错 -Dmaven.multiModuleProjectDirectory system propery is ...

  4. 常州模拟赛d2t1 小X的质数

    题目背景 小 X 是一位热爱数学的男孩子,在茫茫的数字中,他对质数更有一种独特的 情感.小 X 认为,质数是一切自然数起源的地方. 题目描述 在小 X 的认知里,质数是除了本身和 1 以外,没有其他因 ...

  5. leetcode 144 先序遍历和中序遍历差不多

    这是只写了先序遍历的非递归代码 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode * ...

  6. 转 Linux命令-文件管理命令

    http://jingyan.baidu.com/article/9113f81bc1c7a72b3214c7d3.html Linux命令-文件管理命令 浏览:4118 | 更新:2012-11-1 ...

  7. Google解决跨域

    1.添加   --disable-web-security --user-data-dir=D:\tmp 2.在D的根目录新建tmp文件夹

  8. (1)Swing创建窗体

    本系列使用Intellij IDEA 2017.3.4版本 一.运行窗体 1. 2. 3. 4. 5. 6. 给JPanel起个名字 -如From 7. 8. 9. 生成 import javax.s ...

  9. HDU - 5584 LCM Walk (数论 GCD)

    A frog has just learned some number theory, and can't wait to show his ability to his girlfriend. No ...

  10. Maven错误 diamond operator is not supported in -source 1.5 (use -source 7 or higher to enable diamond operator)问题解决

    如果在Maven构建时出现: diamond operator is not supported in -source 1.5 (use -source 7 or higher to enable d ...