【JavaScript高级程序设计】4、引用类型 (2)

1、对于数组的操作方法

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>

    <script type="text/javascript">
    
        var colors = ["red", "green", "blue"];
        //concat 的作用就是把原来的数组拷贝一个副本出来然后组建一个新的副本
        var colors2 = colors.concat("yellow", ["black", "brwon"]);
        
        alert(colors);
        alert(colors2);
        
        //关于slice方法,就是能够基于当前数组中的一或多个项创建一个新数组
        var colors3 = colors2.slice(1);
        var colors4 = colors2.slice(1, 4); 
        
        alert(colors3);
        alert(colors4);
    
    </script>

</head>

<body>
</body>
</html>

 

显示结果:

 

1.1 查找数组中项的位置

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>

    <script>
        var numbers = [2,3,5,1,5,12,1,4,12,1,4,3,1,41,4,234,5];
        var numbers2 = [5,4,3,2,1];
        //indexof是用来求项在数组中所在的索引的位置的
        alert(numbers2.indexOf(0));        //-1 找不到
        /*
        alert(numbers2.indexOf(1));        //4
        alert(numbers2.indexOf(2));        //3
        alert(numbers2.indexOf(3));        //2
        alert(numbers2.indexOf(4));        //1
        alert(numbers2.indexOf(5));            //0
        */
        //这个是从后往前遍历,但是显示从后往前第一个出现的位置
        alert(numbers.indexOf(4));    //7, 从前往后第一个出现
        alert(numbers.lastIndexOf(4));    //14
        
        //第二个参数表示查找的起点索引位置,从第4号索引开始,包含第4号索引
        alert(numbers.indexOf(5, 4));     //4
        alert(numbers.lastIndexOf(4, 4)); // -1  ,从第4号开始往前面索引,查找不到
    
        //这里比较,就算是地址也要一样
        var person = { name: "Nicholas" };
        var people = [{ name: "Nicholas" }];
        var morePeople = [person];
        
        alert(people.indexOf(person));     //-1
        alert(morePeople.indexOf(person)); //0
            
    </script>

</head>

<body>
</body>
</html>

显示结果:

 

1.2 迭代方法

一共5个迭代方法

  1. every():对数组中的每一项运行给定函数,如果该函数对每一项都返回true,则返回true
  2. filter():对数组中的每一项运行给定函数,返回该函数对每一项都返回true的项组成的数组
  3. forEach():对数组中的每一项运行给定函数。这个方法没有返回值。
  4. map():对数组中的每一项运行给定函数,返回每次函数调用的结果组成的数组。
  5. some():对数组中的每一项运行给定函数,如果该函数对任一项返回true,则返回true。

 

关于forEach方法

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>

    <script type="text/javascript">
    
        var numbers = [1,2,4,1,4,3,1,23,12,31,12,2,5,1,4];
        
        numbers.forEach(
                            function(item, index, array)
                            {
                                var temp = item *index / 2;
                                array[index] = temp;
                            }
                        );
        
        
        alert("数组长度是:" + numbers.length);
        alert(numbers.join("##"));
    
    </script>

</head>

<body>
</body>
</html>

 

结果:

 

posted @ 2016-07-03 21:22  cutter_point  阅读(107)  评论(0)    收藏  举报