ECMAScript2019
对象新增方法:Object.fromEntries()
// 二维数组转为对象
const result = Object.fromEntries([
['name', '尚硅谷'],
['xueke', '前端']
])
console.log(result)
// Map转为对象
const m = new Map()
m.set('name', '尚硅谷')
m.set('xueke', '前端')
const result2 = Object.fromEntries(m)
console.log(result2)
// Object.entries (ES8):对象转为二维数组
const arr = Object.entries({
name: '尚硅谷',
xueke: '前端'
})
console.log(arr)
结果:
字符串新增方法:trimStart() 与 trimEnd()
let str = ' iloveyou '
console.log(str.trim()) // 'iloveyou'
console.log(str.trimStart()) // 'iloveyou '
console.log(str.trimEnd()) // ' iloveyou'
数组新增方法:flat() 与 flatMap()
flat
将多维数组转化为低位数组flatMap
就是flat
与map
的结合
// 二维数组
const arr = [1, 2, 3, 4, [5, 6]]
console.log(arr.flat()) // (6) [1, 2, 3, 4, 5, 6]
// 三维数组
const arr2 = [1, 2, 3, 4, [5, 6, [7, 8, 9]]]
// 参数为深度, 是一个数字
console.log(arr2.flat(2)) // (9) [1, 2, 3, 4, 5, 6, 7, 8, 9]
let arr3 = [1, 2, 3, 4], result
/*
map() 方法返回一个新数组,数组中的元素为原始数组元素调用函数处理后的值。
map() 方法按照原始数组元素顺序依次处理元素。
注意:map() 不会对空数组进行检测。
注意:map() 不会改变原始数组。
*/
result = arr3.map(item => item * 10)
console.log(result) // (4) [10, 20, 30, 40]
result = arr3.flatMap(item => [item * 10])
console.log(result) // (4) [10, 20, 30, 40]
Symbol新增属性:Symbol.prototype.description
let s = Symbol('厉害了')
console.log(s.description) // 厉害了