ES6(七)对象扩展
对象扩展 // 简洁表示法 let a = 1 let k = 2 let es5 = { a: a, k: k } let es6 = { a, k } console.log(es5) console.log(es6) let oldMethod = { hello: function () { console.log('hello') } } let es6 = { hello () { console.log('hello') } } oldMethod.hello() es6.hello() // 属性表达式(使用变量作为key) let a = 'b' let es5 = { a: 'c' } let es6 = { [a]: 'c' } // {a: "c"} console.log(es5) // {b: "c"} console.log(es6) // 是否相等 // true console.log('字符串比较', Object.is('abc', 'abc')) // false console.log('数组比较', Object.is([1, 2, 3], [2, 3, 4])) let obj1 = {a: 'a'} let obj2 = {b: 'b'} // false console.log('对象比较', Object.is(obj1, obj2)) // 合并对象 属于浅拷贝 {a: "a", b: "b"} console.log('拷贝对象', Object.assign(obj1, obj2)) let obj3 = Object.assign(obj1, obj2) obj1 = {a: 99} // 深拷贝 {a: "a", b: "b"} console.log('深拷贝', JSON.parse(JSON.stringify(obj3))) // {a: 99} console.log(obj1) // 循环对象 for (let [key, value] of Object.entries(obj3)) { console.log([key, value]) } // 扩展运算符 let {a, b, ...c} = { a: 'hello', b: 'world', c: 'china', d: 'chengdu', e: 'chongqing' } console.log(a) console.log(b) // {c: "china", d: "chengdu", e: "chongqing"} console.log(c)
对象循环
let myData = [ { name: "公告区", todayposts: "0", fid: "2" }, { name: "产品动态", todayposts: "0", fid: "58" }, { name: "点小融专区", todayposts: "0", fid: "72" }, { name: "点融", todayposts: "0", fid: "73" } ]; Object.keys(myData).map(key => { console.log(myData[key]); });