<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>js数据类型Set Map 循环for_in_of</title> </head> <body> <script> let s = "hello"; for(let c of s) { console.log(c); // h e l l o } var set = new Set(); // 集合,有自动去重的功能。 // console.log("set:", set); set.add("a").add("b").add("d").add("a"); console.log("set:", set); for (let v of set) { console.log(v); // a b d } console.log("--------------------"); var map = new Map(); console.log("map:", map); map.set("a",1).set("b",2).set(999,3); console.log("map:", map); for(let [k,v] of map) {// map映射,类似于python字典 console.log(k,v); // a 1 // b 2 // 999 3 } var obj = {1:"hello", 2:"world"}; // for(let o of obj){// Uncaught TypeError: obj is not iterable // console.log(o); // } for(let key in obj){ console.log(key, obj[key]); // 1 hello // 2 world } </script> </body> </html>
js数据类型Set Map 循环for_in_of
于 2019-01-18 17:01:30 首次发布
