Iterating through an array

Source: Internet
Author: User
Tags new set

7.0 for-of: Traversing complex data types 7.0.1 es5--for-in traversing arrays

Used primarily to traverse JSON objects

let arr = [‘a‘,‘b‘,‘c‘,‘d‘];for(let i in arr){    console.log(arr[i]);}//注释:for(创建一个变量);in(in要遍历的复杂数据类型)
7.0.2 es6--for-of
遍历数组:let arr = [‘a‘,‘b‘,‘c‘,‘d‘];for(let item of arr){    console.log(arr[item]);}//注释:for(创建一个变量);of(of要遍历的复杂数据类型)遍历集合:let s = new Set [‘a‘,‘b‘,‘c‘,‘d‘];for(let item of s){    console.log(arr[item]);}遍历字符串:let str = ‘hello world‘;for(let item of str){    console.log(arr[item]);}遍历映射:let m = new Map[[‘a‘,‘b‘],[‘c‘,‘d‘]];for(let item of m){    console.log(arr[item]);}let m = new Map[[‘a‘,‘b‘],[‘c‘,‘d‘]];for(let [key,value] of m){    console.log(key,value);}
7.0.3 Extension: The Walker generation function (used with for-of). Keys (): Traversal key. VALUES (): Iterates over values, not for arrays. Entries (): Traversing Keys and values
let arr = [‘a‘,‘b‘,‘c‘,‘d‘];for(let item of arr.keys()){    console.log(item);}let arr = [‘a‘,‘b‘,‘c‘,‘d‘];for(let item of arr.values()){    console.log(item);}let arr = [‘a‘,‘b‘,‘c‘,‘d‘];for(let item of arr.entries()){    console.log(item);}let arr = [‘a‘,‘b‘,‘c‘,‘d‘];for(let [k,v] of arr.entries()){    console.log(k,v);}

Iterating through an array

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.