zoukankan      html  css  js  c++  java
  • 2017-5-31数组的操作方法

    数组的操作方法

    • 数组的操作方法

      • 1.concat()方法可以基于当前数组中的所有项创建一个新数组.

      • 2.slice()方法可以基于当前数组中的一个或者多个项创建一个新数组.

      • 3.splice()方法的主要用途是向数组的中部插入项,并返回修改的数组

    • 位置方法

      • 数组实例中有两个位置方法:indexOf()和lastIndexOf()

    • 迭代方法

      • every():对数组中的每一项运行给定函数,如果该函数对每一项都返回true,则返回true;

      • filter():对数组中的每一项运行给定函数,返回该函数会返回true的项组成的数组

      • forEach():对数组中的每一项运行给定函数,没有返回值.(本质上与for本地循环相似)

      • map():对数组中的每一项运行给定函数,返回函数每次调用的结果组成的数组.

      • some():对数组中的每一项运行给定函数,如果该函数对任一项返回true,则返回true;

    • 归并方法

      • reduce()和reduceRight(),这两个方法都会迭代数组的所有项,然后构建一个最终的返回值.对数组进行遍历

    1. /** 
    2. * Created by Administrator on 2017-5-30. 
    3. *
    4. /* 
    5. * 
    6. * **数组的操作方法 
    7. * 1.concat()方法可以基于当前数组中的所有项创建一个新数组. 
    8. * 2.slice()方法可以基于当前数组中的一个或者多个项创建一个新数组. 
    9. * 3.splice()方法的主要用途是向数组的中部插入项,并返回修改的数组 
    10. * 
    11. * 
    12. * *
    13.  
    14. //1.concat()方法可以基于当前数组中的所有项创建一个新数组 
    15. /* 
    16. * 原理:concat()方法会基于当前的数组,创建一个副本,然后将接受的参数添加到这个副本的末尾,最后返回新构建的这个数组 
    17. * 无参:在concat()方法中没有传递参数的情况下,他只是复制了当前这个数组的副本 
    18. * 有参:有参数的时候,如果传递是一个或者多个数组,则该方法会将这些数组中的每一项都添加到结果数组中 
    19. * *
    20. var colors = ["red","blue","green"]; 
    21. var colors2 = colors.concat("yellow",["black","brown"]); 
    22. console.log(colors2);//["red", "blue", "green", "yellow", "black", "brown"
    23.  
    24. //2.slice()方法可以基于当前数组中的一个或者多个项创建一个新数组 
    25. /* 
    26. * 传参:接受一个或者两个参数,即从其实位置到结束位置 
    27. * 传一个参数:则返回从该参数的指定位置一直到当前数组末尾的所有项 
    28. * 传两个参数:则该方法返回起始和结束位置之间的项(但不包括结束位置),注意slice()方法不会影响原始数组; 
    29. * 如果slice()方法中参数中有负数,则用数组长度加上该数来确定相应的位置 
    30. * *
    31. var colors = ["red","green","blue","yellow","purple"]; 
    32. var colors2 = colors.slice(1);//["green", "blue", "yellow", "purple"
    33. var colors3 = colors.slice(2,4);// ["blue", "yellow"
    34. var colors4 = colors.slice(-4,-1);//相当于(1,4),["green", "blue", "yellow"
    35. console.log(colors2,colors3,colors4); 
    36.  
    37. //3.splice()方法的3种操作方法 
    38. //删除:可以删除任意数量的项,只需指定2个参数:要删除的第一项的位置和要删除的项数 
    39. var student = ["武松","宋江","鲁智深","王力宏"]; 
    40. var student2 = student.splice(2,3); 
    41. console.log(student2,student); 
    42.  
    43.  
    44.  
    45. /* 
    46. * 插入:可以向指定位置插入任意数量的项,只需提供3个参数:起始位置,0(要删除的项数),和要插入的项, 
    47. * 如果要插入多个项,可以再传入第4,第5,以至任意多个项 
    48. * 传参:splice("起始位置","要删除的项数","要插入任意数量的项"
    49. * *
    50. var student3 = student.splice(1,0,"yellow","green","blue"); //student3是一个数组 
    51. console.log(student3,student); 
    52.  
    53. /* 
    54. * 替换:可以向指定位置插入任意数量的项,且同时删除任意数量的项 
    55. * 传参:splice("起始位置","要删除的项数","要插入任意数量的项"
    56. * 
    57. * *
    58. var book = ["红楼梦","水浒传","西游记","三国演义"]; 
    59. var book2 = book.splice(1,1,"史记");// 
    60. console.log(book,book2);//["红楼梦", "史记", "西游记", "三国演义"] ["水浒传"
    61.  
    62.  
    63.  
    64. /* 
    65. * *********************************5.2.7位置方法*************** 
    66. * 数组实例中有两个位置方法:indexOf()和lastIndexOf() 
    67. * 
    68. * *
    69.  
    70. //1.indexOf()方法:从数组的开头(位置0)开始向后查找 
    71. var numbers = [1,2,3,4,5,4,3,2,1]; 
    72. console.log(numbers.indexOf(4));//找的的索引位置是3 
    73.  
    74. //2.lastIndexOf()方法:从数组的末尾开始向前查找 
    75. console.log(numbers.lastIndexOf(4));//5 
    76.  
    77. var person = {name : "武松"}; //一个对象 
    78. var people = [{name : "武松"}];//数组里面的元素是一个对象 
    79. var morePeople = [person];//morePeople 被初始化成person数组里面的一个对象; 
    80. console.log(people.indexOf(person),morePeople.indexOf(person)); 
    81.  
    82. /* 
    83. ******************************5.2.8迭代方法****************** 
    84. * javaScript语法中,定义了5个迭代方法: 
    85. * 
    86. * *
    87.  
    88.  
    89. /* 
    90. * every()方法 
    91. * 对数组中的每一项运行给定函数,如果该函数对每一项都返回true,则返回true; 
    92. * 参数:参数1:数组项的值;参数2:该项在数组中的位置;参数3:数组对象本身; 
    93. * *
    94. var numbers = [1,2,3,4,5,4,3,2,1]; 
    95. var everyResult = numbers.every(function(item,index,array){ 
    96. return (item > 0);//返回true ,取出在numbers数组中的每一项进行>0的比较; 
    97. }); 
    98. console.log(everyResult); 
    99.  
    100. /* 
    101. * filter()方法 
    102. * 对数组中的每一项运行给定函数,返回该函数会返回true的项组成的数组; 
    103. * 参数:参数1:数组项的值;参数2:该项在数组中的位置;参数3:数组对象本身; 
    104. * *
    105. var filterResult = numbers.filter(function(item,index,array){ 
    106. return (item > 2); 
    107. }); 
    108. console.log(filterResult);//[3, 4, 5, 4, 3] 
    109.  
    110. /* 
    111. * forEach()方法: 
    112. * 对数组中的每一项,运行传入的函数,这个方法没有返回值,本质上与使用for循环迭代数组一样; 
    113. * 参数:参数1:数组项的值;参数2:该项在数组中的位置;参数3:数组对象本身; 
    114. * *
    115. var numbers = [1,2,3,4,5,4,3,2,1,]; 
    116. numbers.forEach(function(item,index,array){ 
    117. if(item > 2){ 
    118. console.log(item); //3,4,5,4,3;作用类似于for循环 

    119. }); 
    120.  
    121. /* 
    122. * map()方法: 
    123. * 对数组中的每一项运行给定函数,返回每次函数调用的结果组成的数组 
    124. * 参数:参数1:数组项的值;参数2:该项在数组中的位置;参数3:数组对象本身; 
    125. * *
    126. var numbers = [1,2,3,4,5,4,3,2,1]; 
    127. var newNumbers = numbers.map(function(item,index,array){ 
    128. if(item > 3){ 
    129. return item * 2; 

    130. }); 
    131. console.log(newNumbers); //[undefined, undefined, undefined, 8, 10, 8, undefined, undefined, undefined] 
    132.  
    133. /* 
    134. * some()方法 
    135. * 对数组中的每一项运行给定函数,如果该函数对任一项返回true,则返回true 
    136. * 参数:参数1:数组项的值;参数2:该项在数组中的位置;参数3:数组对象本身; 
    137. * *
    138. var numbers = [1,,2,3,4,5,4,3,2,1]; 
    139. var result = numbers.some(function(item,index,array){ 
    140. return (item > 13); 
    141.  
    142. }); 
    143. console.log(result); 
    144.  
    145. /* 
    146. * *************************5.2.9归并方法************** 
    147. * 两个归并数组的方法:reduce()方法和reduceRight()方法 
    148. * 这个两个方法都会迭代数组的所有项,然后构建一个最终返回的值 
    149. * 1.reduce()方法从数组的第一项开始,逐个遍历到最后 
    150. * 2.reduceRight()方法从数组的最后一项开始,向前遍历到第一项; 
    151. * *
    152. var values = [1,2,3,4,5]; 
    153. //求出了数组中的每一项之和 
    154. //reduce()方法 
    155. var sum = values.reduce(function(prev,cur,index,array){ 
    156. return prev + cur;//prev表示第一项,cur表示第二项 
    157. }); 
    158. console.log(sum); //15 
    159.  
    160. //reduceRight()方法 
    161. var sum2 = values.reduceRight(function(number1,number2,index,array){ 
    162. return number1 + number2; 
    163. }); 
    164. console.log(sum2); //15 
    165.  
    166.  
    167.  
    每天叫醒的不是闹钟,而是梦想
  • 相关阅读:
    sqlserver怎么将excel表的数据导入到数据库中
    TCP端口状态说明ESTABLISHED、TIME_WAIT
    服务器上安装FileZilla Server连接时报You appear to be behind a NAT router. Please configure the passive mode settings and forward a range of ports in your router.
    启动项目报错:502 Server dropped connection The following error occurred while trying to access http://localhost:8080/TestDemo:
    [ngx-formly] Dynamically set Model Properties with Angular Formly Expressions
    [ngx-formly] Dynamically disable a Form field with Angular Formly
    [ngx-formly] Using field hooks to listening value changes
    [Git] Undo a Commit that has Already Been Pushed
    [Git] Recover Local Changes from `git reset --hard` with `git reflog`
    [Git] Use and Compare the Different git Reset Options: --hard, --soft, and --mixed
  • 原文地址:https://www.cnblogs.com/shuiyaodongwu310988929/p/6922624.html
Copyright © 2011-2022 走看看