数组的操作方法
数组的操作方法
1.concat()方法可以基于当前数组中的所有项创建一个新数组.
2.slice()方法可以基于当前数组中的一个或者多个项创建一个新数组.
3.splice()方法的主要用途是向数组的中部插入项,并返回修改的数组
位置方法
数组实例中有两个位置方法:indexOf()和lastIndexOf()
迭代方法
every():对数组中的每一项运行给定函数,如果该函数对每一项都返回true,则返回true;
filter():对数组中的每一项运行给定函数,返回该函数会返回true的项组成的数组
forEach():对数组中的每一项运行给定函数,没有返回值.(本质上与for本地循环相似)
map():对数组中的每一项运行给定函数,返回函数每次调用的结果组成的数组.
some():对数组中的每一项运行给定函数,如果该函数对任一项返回true,则返回true;
归并方法
reduce()和reduceRight(),这两个方法都会迭代数组的所有项,然后构建一个最终的返回值.对数组进行遍历
- /**
- * Created by Administrator on 2017-5-30.
- */
- /*
- *
- * **数组的操作方法
- * 1.concat()方法可以基于当前数组中的所有项创建一个新数组.
- * 2.slice()方法可以基于当前数组中的一个或者多个项创建一个新数组.
- * 3.splice()方法的主要用途是向数组的中部插入项,并返回修改的数组
- *
- *
- * */
-
- //1.concat()方法可以基于当前数组中的所有项创建一个新数组
- /*
- * 原理:concat()方法会基于当前的数组,创建一个副本,然后将接受的参数添加到这个副本的末尾,最后返回新构建的这个数组
- * 无参:在concat()方法中没有传递参数的情况下,他只是复制了当前这个数组的副本
- * 有参:有参数的时候,如果传递是一个或者多个数组,则该方法会将这些数组中的每一项都添加到结果数组中
- * */
- var colors = ["red","blue","green"];
- var colors2 = colors.concat("yellow",["black","brown"]);
- console.log(colors2);//["red", "blue", "green", "yellow", "black", "brown"]
-
- //2.slice()方法可以基于当前数组中的一个或者多个项创建一个新数组
- /*
- * 传参:接受一个或者两个参数,即从其实位置到结束位置
- * 传一个参数:则返回从该参数的指定位置一直到当前数组末尾的所有项
- * 传两个参数:则该方法返回起始和结束位置之间的项(但不包括结束位置),注意slice()方法不会影响原始数组;
- * 如果slice()方法中参数中有负数,则用数组长度加上该数来确定相应的位置
- * */
- var colors = ["red","green","blue","yellow","purple"];
- var colors2 = colors.slice(1);//["green", "blue", "yellow", "purple"]
- var colors3 = colors.slice(2,4);// ["blue", "yellow"]
- var colors4 = colors.slice(-4,-1);//相当于(1,4),["green", "blue", "yellow"]
- console.log(colors2,colors3,colors4);
-
- //3.splice()方法的3种操作方法
- //删除:可以删除任意数量的项,只需指定2个参数:要删除的第一项的位置和要删除的项数
- var student = ["武松","宋江","鲁智深","王力宏"];
- var student2 = student.splice(2,3);
- console.log(student2,student);
-
-
-
- /*
- * 插入:可以向指定位置插入任意数量的项,只需提供3个参数:起始位置,0(要删除的项数),和要插入的项,
- * 如果要插入多个项,可以再传入第4,第5,以至任意多个项
- * 传参:splice("起始位置","要删除的项数","要插入任意数量的项")
- * */
- var student3 = student.splice(1,0,"yellow","green","blue"); //student3是一个数组
- console.log(student3,student);
-
- /*
- * 替换:可以向指定位置插入任意数量的项,且同时删除任意数量的项
- * 传参:splice("起始位置","要删除的项数","要插入任意数量的项")
- *
- * */
- var book = ["红楼梦","水浒传","西游记","三国演义"];
- var book2 = book.splice(1,1,"史记");//
- console.log(book,book2);//["红楼梦", "史记", "西游记", "三国演义"] ["水浒传"]
-
-
-
- /*
- * *********************************5.2.7位置方法***************
- * 数组实例中有两个位置方法:indexOf()和lastIndexOf()
- *
- * */
-
- //1.indexOf()方法:从数组的开头(位置0)开始向后查找
- var numbers = [1,2,3,4,5,4,3,2,1];
- console.log(numbers.indexOf(4));//找的的索引位置是3
-
- //2.lastIndexOf()方法:从数组的末尾开始向前查找
- console.log(numbers.lastIndexOf(4));//5
-
- var person = {name : "武松"}; //一个对象
- var people = [{name : "武松"}];//数组里面的元素是一个对象
- var morePeople = [person];//morePeople 被初始化成person数组里面的一个对象;
- console.log(people.indexOf(person),morePeople.indexOf(person));
-
- /*
- ******************************5.2.8迭代方法******************
- * javaScript语法中,定义了5个迭代方法:
- *
- * */
-
-
- /*
- * every()方法
- * 对数组中的每一项运行给定函数,如果该函数对每一项都返回true,则返回true;
- * 参数:参数1:数组项的值;参数2:该项在数组中的位置;参数3:数组对象本身;
- * */
- var numbers = [1,2,3,4,5,4,3,2,1];
- var everyResult = numbers.every(function(item,index,array){
- return (item > 0);//返回true ,取出在numbers数组中的每一项进行>0的比较;
- });
- console.log(everyResult);
-
- /*
- * filter()方法
- * 对数组中的每一项运行给定函数,返回该函数会返回true的项组成的数组;
- * 参数:参数1:数组项的值;参数2:该项在数组中的位置;参数3:数组对象本身;
- * */
- var filterResult = numbers.filter(function(item,index,array){
- return (item > 2);
- });
- console.log(filterResult);//[3, 4, 5, 4, 3]
-
- /*
- * forEach()方法:
- * 对数组中的每一项,运行传入的函数,这个方法没有返回值,本质上与使用for循环迭代数组一样;
- * 参数:参数1:数组项的值;参数2:该项在数组中的位置;参数3:数组对象本身;
- * */
- var numbers = [1,2,3,4,5,4,3,2,1,];
- numbers.forEach(function(item,index,array){
- if(item > 2){
- console.log(item); //3,4,5,4,3;作用类似于for循环
- }
- });
-
- /*
- * map()方法:
- * 对数组中的每一项运行给定函数,返回每次函数调用的结果组成的数组
- * 参数:参数1:数组项的值;参数2:该项在数组中的位置;参数3:数组对象本身;
- * */
- var numbers = [1,2,3,4,5,4,3,2,1];
- var newNumbers = numbers.map(function(item,index,array){
- if(item > 3){
- return item * 2;
- }
- });
- console.log(newNumbers); //[undefined, undefined, undefined, 8, 10, 8, undefined, undefined, undefined]
-
- /*
- * some()方法
- * 对数组中的每一项运行给定函数,如果该函数对任一项返回true,则返回true
- * 参数:参数1:数组项的值;参数2:该项在数组中的位置;参数3:数组对象本身;
- * */
- var numbers = [1,,2,3,4,5,4,3,2,1];
- var result = numbers.some(function(item,index,array){
- return (item > 13);
-
- });
- console.log(result);
-
- /*
- * *************************5.2.9归并方法**************
- * 两个归并数组的方法:reduce()方法和reduceRight()方法
- * 这个两个方法都会迭代数组的所有项,然后构建一个最终返回的值
- * 1.reduce()方法从数组的第一项开始,逐个遍历到最后
- * 2.reduceRight()方法从数组的最后一项开始,向前遍历到第一项;
- * */
- var values = [1,2,3,4,5];
- //求出了数组中的每一项之和
- //reduce()方法
- var sum = values.reduce(function(prev,cur,index,array){
- return prev + cur;//prev表示第一项,cur表示第二项
- });
- console.log(sum); //15
-
- //reduceRight()方法
- var sum2 = values.reduceRight(function(number1,number2,index,array){
- return number1 + number2;
- });
- console.log(sum2); //15
-
-
-