如果你看完了你会体会到一个人能有多无聊,这东西都能看完!!??

大概js有以下几种循环遍历的方法:

1 let arr = ['aaa','bbb','ccc']

2 let obj = {a:'aaa',b:'bbb',c:'ccc'}

3 let deepObj = {a:'aaa', b:{c:'ccc'}}

4

5 // foriFn(arr);// 经典for循环

6 // index :>> 0

7 // item :>> aaa

8 // index :>> 1

9 // item :>> bbb

10 // index :>> 2

11 // item :>> ccc

12

13 // foriFnPlus(arr);// 经典for循环稍微优化一下子

14 // index :>> 0

15 // item :>> aaa

16 // index :>> 1

17 // item :>> bbb

18 // index :>> 2

19 // item :>> ccc

20

21 // forEachFn(arr);// forEach遍历数组 return不会终止循环,return相当于for循环里的continue

22 // index :>> 0

23 // item :>> aaa

24 // index :>> 1

25 // item :>> bbb

26 // index :>> 2

27 // item :>> ccc

28

29 // forinFn(arr)// forin循环 循环时候得到的key时字符串类型的

30 // index :>> 0

31 // typeof index :>> string

32 // item :>> aaa

33 // index :>> 1

34 // typeof index :>> string

35 // item :>> bbb

36 // index :>> 2

37 // typeof index :>> string

38 // item :>> ccc

39 // forinFn(obj)//forin循环 可以遍历对象

40 // index :>> a

41 // typeof index :>> string

42 // item :>> aaa

43 // index :>> b

44 // typeof index :>> string

45 // item :>> bbb

46 // index :>> c

47 // typeof index :>> string

48 // item :>> ccc

49 // forinFn(deepObj)//forin循环 可以遍历对象 只遍历一层

50 // index :>> a

51 // typeof index :>> string

52 // item :>> aaa

53 // index :>> b

54 // typeof index :>> string

55 // item :>> { c: 'ccc' }

56

57 // forofFn(arr)// forof循环 循环时候没有index

58 // index :>> 0

59 // item :>> aaa

60 // index :>> 1

61 // item :>> bbb

62 // index :>> 2

63 // item :>> ccc

64

65 // doWhileFn(arr)// do While循环

66 // item :>> aaa

67 // item :>> bbb

68 // item :>> ccc

69 // whiledoFn(arr)// while do循环

70 // item :>> aaa

71 // item :>> bbb

72 // item :>> ccc

73

74 // mapFn(arr)// map循环 return不会终止循环,return相当于for循环里的continue

75 // index :>> 0

76 // item :>> aaa

77 // 0

78 // index :>> 1

79 // item :>> bbb

80 // index :>> 2

81 // item :>> ccc

82

83 // filterFn(arr)// filter循环 return不会终止循环,return相当于for循环里的continue

84 // index :>> 0

85 // item :>> aaa

86 // 0

87 // index :>> 1

88 // item :>> bbb

89 // index :>> 2

90 // item :>> ccc

91

92

93 // reduceFn(arr)// reduce方法 特殊应用场景使用 和上边的循环不一样

94 // i :>> aaa

95 // j :>> bbb

96 // i :>> aaabbb

97 // j :>> ccc

98 // res :>> aaabbbccc

99

100 // entriesFn(arr)// 遍历器entries循环

101 // Array.isArray(val) :>> true

102 // val :>> [ 0, 'aaa' ]

103 // index :>> 0

104 // item :>> aaa

105 // val :>> [ 1, 'bbb' ]

106 // index :>> 1

107 // item :>> bbb

108 // val :>> [ 2, 'ccc' ]

109 // index :>> 2

110 // item :>> ccc

111 // entriesFnPlus(arr)// 遍历器entries循环优化

112 // Array.isArray(val) :>> true

113 // val :>> [ 0, 'aaa' ]

114 // index :>> 0

115 // item :>> aaa

116 // Array.isArray(val) :>> true

117 // val :>> [ 1, 'bbb' ]

118 // index :>> 1

119 // item :>> bbb

120 // Array.isArray(val) :>> true

121 // val :>> [ 2, 'ccc' ]

122 // index :>> 2

123 // item :>> ccc

124

125 // keysValuesFn(arr)// 用数组的 arr.keys() arr.values() 方法遍历

126 // index :>> 0

127 // index :>> 1

128 // index :>> 2

129 // item :>> aaa

130 // item :>> bbb

131 // item :>> ccc

132

133 // ObjectKeysValuesFn(arr)// 用对象的 Object.keys(arr)、Object.values(arr)方法返回的字符串数组 遍历

134 // index :>> 0

135 // index :>> 1

136 // index :>> 2

137 // item :>> aaa

138 // item :>> bbb

139 // item :>> ccc

140 // ObjectKeysValuesFn(obj)// 可以遍历对象

141 // index :>> a

142 // index :>> b

143 // index :>> c

144 // item :>> aaa

145 // item :>> bbb

146 // item :>> ccc

147 // ObjectKeysValuesFn(deepObj)// 可以遍历对象 只遍历一层

148 // index :>> a

149 // index :>> b

150 // item :>> aaa

151 // item :>> { c: 'ccc' }

152

153 /**

154 * 经典for循环 据说性能最高

155 * @param {Array} arr

156 */

157 function foriFn(arr){

158 for(let i=0; i

159 console.log('index :>> ', i);

160 console.log('item :>> ', arr[i]);

161 }

162 }

163

164 /**

165 * 经典for循环 优化

166 * 将数组的length缓存起来避免每次都取获取数组的length

167 * @param {Array} arr

168 */

169 function foriFnPlus(arr){

170 for(let i=0,len=arr.length; i

171 console.log('index :>> ', i);

172 console.log('item :>> ', arr[i]);

173 }

174 }

175

176 /**

177 * forEach遍历数组

178 * return不会终止循环,return相当于for循环里的continue

179 * @param {Array} arr

180 */

181 function forEachFn(arr){

182 arr.forEach((item,index,a)=>{

183 console.log('index :>> ', index);

184 console.log('item :>> ', item);

185 if(index){

186 return;

187 }

188 })

189 }

190

191 /**

192 * forin循环

193 * 可以循环数组可以循环对象

194 * @param {Object,Array} arr

195 */

196 function forinFn(arr){

197 for (const idx in arr) {

198 if (arr.hasOwnProperty(idx)) {

199 const item = arr[idx];

200 console.log('index :>> ', idx);

201 console.log('typeof index :>> ', typeof idx);

202 console.log('item :>> ', item);

203 }

204 }

205 }

206

207 /**

208 * forOf循环

209 * 没给你传index

210 * @param {Array} arr

211 */

212 function forofFn(arr){

213 for (const item of arr) {

214 console.log('index :>> ', arr.indexOf(item));

215 console.log('item :>> ', item);

216 }

217 }

218

219 /**

220 * do while循环

221 * @param {Array} arr

222 */

223 function doWhileFn(arr){

224 do{

225 console.log('item :>> ', arr.shift());

226 }while(arr.length>0)

227 }

228

229 /**

230 * while do 循环

231 * @param {Array} arr

232 */

233 function whiledoFn(arr) {

234 while (arr.length>0) {

235 console.log('item :>> ', arr.shift());

236 }

237 }

238

239 /**

240 * map循环

241 * return不会终止循环,return相当于for循环里的continue

242 * @param {Array} arr

243 */

244 function mapFn(arr){

245 arr.map((v,i,a)=>{

246 console.log('index :>> ', i);

247 console.log('item :>> ', v);

248 if(i){

249 return

250 }

251 console.log(i);

252 })

253 }

254

255 /*

256 * filter循环

257 * return不会终止循环,return相当于for循环里的continue

258 * @param {Array} arr

259 */

260 function filterFn(arr){

261 arr.filter((v,i,a)=>{

262 console.log('index :>> ', i);

263 console.log('item :>> ', v);

264 if(i){

265 return

266 }

267 console.log(i);

268 })

269 }

270

271 /**

272 * reduce循环

273 * @param {Array} arr

274 */

275 function reduceFn(arr){

276 let res = arr.reduce((i,j)=>{

277 console.log('i :>> ', i);

278 console.log('j :>> ', j);

279 return i+j

280 })

281 console.log('res :>> ', res);

282 }

283

284 /**

285 * 遍历器循环

286 * @param {Array} arr

287 */

288 function entriesFn(arr){

289 let ets = arr.entries()

290

291 let val = ets.next().value

292 console.log('Array.isArray(val) :>> ', Array.isArray(val));

293 console.log('val :>> ', val);

294 console.log('index :>> ', val[0]);

295 console.log('item :>> ', val[1]);

296

297 val = ets.next().value

298 console.log('val :>> ', val);

299 console.log('index :>> ', val[0]);

300 console.log('item :>> ', val[1]);

301

302 val = ets.next().value

303 console.log('val :>> ', val);

304 console.log('index :>> ', val[0]);

305 console.log('item :>> ', val[1]);

306 }

307

308 /**

309 * 遍历器循环简化版

310 * @param {Array} arr

311 */

312 function entriesFnPlus(arr){

313 let ets = arr.entries()

314 let nxt=ets.next(),val

315 while(!nxt.done){

316 val = nxt.value

317 console.log('Array.isArray(val) :>> ', Array.isArray(val));

318 console.log('val :>> ', val);

319 console.log('index :>> ', val[0]);

320 console.log('item :>> ', val[1]);

321 nxt = ets.next()

322 }

323 }

324

325 /**

326 * 利用数组keys values属性的循环

327 * @param {Array} arr

328 */

329 function keysValuesFn(arr){

330 for (const index of arr.keys()) {

331 console.log('index :>> ', index);

332 }

333 for (const item of arr.values()) {

334 console.log('item :>> ', item);

335 }

336 }

337

338 /**

339 * 利用对象keys values属性的循环

340 * @param {Object} arr

341 */

342 function ObjectKeysValuesFn(arr){

343 for (const index of Object.keys(arr)) {

344 console.log('index :>> ', index);

345 }

346 for (const item of Object.values(arr)) {

347 console.log('item :>> ', item);

348 }

349 }

over

标签:index,arr,遍历,console,log,val,js,item,数组

来源: https://www.cnblogs.com/rainbowLover/p/13269993.html

html怎么遍历数组,js遍历数组有多少种方法相关推荐

  1. JS数组转字符串(3种方法) arrays.join(“-“)把数组使用-分割为字符串

    JS数组转字符串(3种方法) JavaScript 允许数组与字符串之间相互转换.其中 Array 方法对象定义了 3 个方法,可以把数组转换为字符串,如表所示. Array 对象的数组与字符串相互转 ...

  2. JS数组转字符串(3种方法)和字符串转数组(2种)

    一:数组转字符串(3种方法) 同样是数组转字符串,toString(),toLocaleString(),join(),join(',')的区别是什么? JavaScript 允许数组与字符串之间相互 ...

  3. Javascript循环删除数组中元素的3种方法

    本文主要跟大家分享了关于Javascript循环删除数组中元素的几种方法,分享出来供大家参考学习,下面与微点阅读小编一起来看看详细的介绍: 问题 大家在码代码的过程中,经常会遇到在循环中移除指定元素的 ...

  4. php 带建数组转字符串,php数组转换为字符串的两种方法详解【附视频】

    本篇文章主要给大家介绍PHP数组转换为字符串的两种方法.(文章末尾附有对应的视频教程) 第一种方法:使用PHP本身的函数implode来直接将数组转换为字符串. 第二种方法:使用循环遍历数组元素拼接成 ...

  5. 实现数组扁平化的6种方法

    实现数组扁平化的6种方法 扁平化的实现 方法一:普通的递归实现 方法二:利用 reduce 函数迭代 方法三:扩展运算符实现 方法四:split 和 toString 共同处理 方法五:调用 ES6 ...

  6. 【原】动态申请二维数组并释放的三种方法

    在C++中实现变长数组 一般来说,有三种方法来申请多维数组:C的malloc/Free    C++的new/delete    STL容器Vector 1.变长一维数组 这里说的变长数组是指在编译时 ...

  7. 图解|查找数组中最大值的5种方法!

    作者 | 王磊 来源 | Java中文社群(ID:javacn666) 转载请联系授权(微信ID:GG_Stone) 我们在一些特定场景下,例如查询公司员工的最高薪资,以及班级的最高成绩又或者是面试中 ...

  8. java数组循环扩容_Java中实现数组动态扩容的两种方法

    Java中实现数组动态扩容的两种方法 java中初始化一个数组需要定义数组的容量,而在我们使用数组时往往会遇到数组容量不够的情况,此时我们就需要通过动态扩容的方式来来根据需求扩大数组的容量. 我们可以 ...

  9. Java 数组转List的几种方法

    Java 数组转List的几种方法 一.Arrays.asList 二.Collections.addAll 三.Arrays.stream(arr).collect(Collectors.toLis ...

  10. 从19本书中选取五本,并且要求这五本互相不相邻,一共有多少种方法?

    题目: 从19本书中选取五本,并且要求这五本互相不相邻,一共有多少种方法? 解决方案一:挡板问题--插空法 假设当前在书架上已经放好14本书,那么只需要再把剩下五本书插入这些空中即可. 14本书有15 ...

最新文章

  1. Mybatis 实现SQL拦截并在控制台打印SQL和参数
  2. js 拼接html 表格,js合并table单元格(拼table的时候并不知道具体几行几列)
  3. 函数:MySQL中字符串匹配函数LOCATE和POSITION使用方法
  4. kafka topic 目录存放在哪_Kafka系列文章之安装测试-第2篇
  5. Android 系统(87)---常见的内存泄漏原因及解决方法
  6. Centos7 minmal 安装
  7. 网络协议从入门到底层原理(6)应用层 - 域名、DNS、DHCP、HTTP(ABNF、HTTP报文格式、请求方法、头部字段、状态码、跨域)、代理、CDN
  8. 大数据爬虫实习面试题
  9. 可编程的,协议独立的软件交换机(论文阅读)
  10. 中山マミ - 彼女×彼女×彼女 ~今夜はぎゅっと抱きしめてね~
  11. scrum立会报告+燃尽图(第三周第三次)
  12. Cesium渐变色3dtiles白模(视频)
  13. 群晖NAS加AD域时提示用户名或密码错误,但域管理员帐号和密码是对的,并且在电脑上可以正常加域。
  14. 1.11CSS的基本语法
  15. 百度飞桨AI抠图+图片合成
  16. linux服务器抓包分析,抓包分析SSL/TLS连接建立过程总结
  17. UNT413-S905L3机顶盒线刷记录
  18. 使用安全令牌保护 RTMP 流
  19. 基于word2vec的疾病和手术相关词语的相似度计算
  20. 暴力视音频分类检测相关论文

热门文章

  1. java 文曲星猜数字,文曲星里的猜数字代码(原创)
  2. pythonloop是什么意思_python loop 英文问题
  3. 普通路由器改4g路由器_4G宽带随心用,办公娱乐更自由,蒲公英X4C路由器体验|路由器|蒲公英|宽带|wifi|sim...
  4. 通过命令行获取计算机参数,Win32命令行参数的传到和获取
  5. java 单文件上传_java – JIRA中的单个文件上传
  6. python pytest框架
  7. 在vc++里面进行图像处理的时候应该把图形放哪_图形找朋友小班教案
  8. C语言程序设计打鱼还是晒网,C语言编程三天打鱼两天晒网
  9. jQuery+toggle
  10. 从JSP WEB页面往数据库写入出现乱码的一种解决方法