ES8新增對對象快速遍歷的方法segmentfault
Object.keys()數組
Object.entries()學習
ES5spa
let grade = { "lilei": 96, "hanmeimei": 99 } let result = [] for (let k in grade) { result.push(k) } console.log(result) //["lilei", "hanmeimei"]
ES8code
let grade = { "lilei": 96, "hanmeimei": 99 } console.log(Object.keys(grade)) //["lilei", "hanmeimei"]
ES5對象
let grade = { "lilei": 96, "hanmeimei": 99 } let result = [] for (let k in grade) { if(k === 'lilei'){ result.push(k) } } console.log(result) // ["lilei"]
ES8blog
let grade = { "lilei": 96, "hanmeimei": 99 } console.log(Object.keys(grade).filter(item => item === 'lilei')) // ["lilei"] // 返回一個數組,以後能夠對數組進行合併、替換、查找
let grade = { "lilei": 96, "hanmeimei": 99 } console.log(Object.values(grade)) // [96,99] console.log(Object.values(grade).filter(item => item > 97)) //[99]
能夠結合 ES6(十五)—— lterator 使用rem
let grade = { "lilei": 96, "hanmeimei": 99 } for(let [k, v] of Object.entries(grade)){ console.log(k,v) } // lilei 96 // hanmeimei 99
本質依舊是把對象變成可遍歷的,符合lterator
結構的get
let grade = { "lilei": 96, "hanmeimei": 99 } let map1 = new Map(Object.entries(grade)) map1.get("lilei") // 96