JSON数组
Array函数
String学习
matchAll编码
ES5spa
Objectprototype
fromEntriescode
Symbol对象
由于ES10
是对总体原型对象的加强和对语法进行了些修正,没有一块新的内容,是对原来的内容进行升级,细微的东西比较多,因此就写在一块儿了...blog
JSON.stringify
在 ES10
修复了对于一些超出范围的 Unicode
展现错误的问题。由于 JSON
都是被编码成 UTF-8
,因此遇到 0xD800–0xDFFF
以内的字符会由于没法编码成 UTF-8
进而致使显示错误。递归
升级以后:在 ES10
它会用转义字符的方式来处理这部分字符而非编码的方式,这样就会正常显示了。
console.log(JSON.stringify('\u{D800}')) //"\ud800" JSON.stringify('\u{D800}') === '"\\ud800"' // true
flat
里面传参数 arr.flat(depth)
let arr = [1, [2, 3], [4, 5, [6, 7]]] console.log(arr.flat()) // [1, 2, 3, 4, 5, [6, 7]] // 带参数,深度为2 console.log(arr.flat(2)) // [1, 2, 3, 4, 5, 6, 7]
首先什么是map
?
let array = [1, 2, 3] console.log(array.map(item => item * 2)) // [2, 4, 6]
flatMap
至关于map
+flat
,若是须要先对这个数组进行map
再进行flat
操做,就能够直接使用flatMap
console.log(array.flatMap(item => [item * 2])) // [2, 4, 6] // 等价于下面 console.log(array.map(item => [item * 2])) // [[2], [4], [6]] console.log(array.map(item => [item * 2]).flat()) // [2, 4, 6]
MDN
上面有一个例子来讲明二者的不一样
let arr = ['今每天气不错', '', '早上好'] arr.map(s => s.split('')) // [["今", "天", "天", "气", "不", "错"],[""],["早", "上", "好"]] arr.flatMap(s => s.split('')) // ["今", "天", "天", "气", "不", "错", "", "早", "上", "好"]
trim
是字符串去空格方法,这里是控制从左边去空格仍是右边去空格。
let str = ' foo '
ES5
// 首尾的空格都去掉 console.log(str.trim()) // 前面的空格进行去掉 console.log(str.replace(/^\s+/g, '')) // 前面和后面的空格都去掉 console.log(str.replace(/^\s+|\s+$/g, ''))
ES10
// 去掉字符串前面的空格 console.log(str.trimStart()) // or console.log(str.trimLeft()) // 去掉字符串后面的空格 console.log(str.trimEnd()) // or console.log(str.trimRight())
let str = `"foo" and "bar" and "baz"`
如何把foo
,bar
,baz
提取出来?
RegExp.prototype.exec()
with /g
match
方法只能匹配一个,因此要用一个while
循环function select(regExp, str) { const matches = [] // 由于match方法只能匹配一个,因此要用一个while循环 while(true){ const match = regExp.exec(str) if(match === null) break // 将捕获的第一个输入进数组 matches.push(match[1]) } return matches } // 必需要进行全局匹配,否则每次都是从头开始匹配 // 小括号是捕获,只要不是双引号,就捕获进来 console.log(select(/"([^"]*)"/g, str)) // ["foo", "bar", "baz"]
这样写比较繁琐,还有没有别的方法?
String.prototype.match()
with /g
console.log(str.match(/"([^"]*)"/g)) // [""foo"", ""bar"", ""baz""]
上面的方法会把全部的捕获都扔掉,只取完整的匹配,因此每一个都多了双引号
String.prototype.replace()
function select (regExp, str) { const matches = [] // replace的高级用法,第二个参数能够传一个函数 str.replace(regExp, function (all, first) { matches.push(first) }) return matches } console.log(select(/"([^"]*)"/g, str)) // ["foo", "bar", "baz"]
let str = `"foo" and "bar" and "baz"` function select(regExp, str) { const matches = [] // matchAll返回的是可遍历的全部匹配结果 RegExpStringIterator{} // 咱们对全部的结果作遍历 for (const match of str.matchAll(regExp)) { matches.push(match[1]) } return matches } console.log(select(/"([^"]*)"/g, str)) // ["foo", "bar", "baz"]
对象和数组怎么互相转换?
entries
fromEntries
fromEntries
和entries
是相对的,而且须要遵循固定的格式。
// 数组转对象 —— entries const obj = {'foo':'a','bar':'b'} console.log(Object.entries(obj)) //[["foo", "a"],["bar", "b"]] // 数组转对象 —— fromEntries const array = ['foo','bar','baz'] console.log(Object.fromEntries(array)) // 这样会报错必须是固定格式 // 正确写法: const array = [["foo", "a"],["bar", "b"]] console.log(Object.fromEntries(array)) // {foo: "a", bar: "b"}
若是要访问[['foo', 1], ['bar', 2]]
数组中bar
的值怎么办?
ES5
const arr = [['foo', 1], ['bar', 2]] console.log(arr[1][1]) // 2
ES10
const arr = [['foo', 1], ['bar', 2]] const obj = Object.fromEntries(arr) console.log(obj) // {foo: 1, bar: 2} console.log(obj.bar) // 2
下面对象把键的字符串长度为3的内容保留下来,其余删除,怎么作?
const obj = { abc: 1, def: 2, ghdsk: 3 } // 而后将数组再转化成对象 let res = Object.fromEntries( // entries先把object对象变成数组,而后对数组进行过滤 Object.entries(obj).filter(([key,val]) => key.length === 3) ) console.log(res) // {abc: 1, def: 2}
以前catch
后面必需要有e
异常变量这个参数,有时候并无必要
try { ... } catch (e) { ... }
ES10
以后括号能够删掉
try { ... } catch { ... }
咱们知道,Symbol
的描述只被存储在内部的 [[Description]]
,没有直接对外暴露,咱们只有调用 Symbol
的 toString()
时才能够读取这个属性:
const name = Symbol('My name is axuebin') console.log(name.toString()) // Symbol(My name is axuebin) console.log(name) // Symbol(My name is axuebin) console.log(name === 'Symbol(My name is axuebin)') // false console.log(name.toString() === 'Symbol(My name is axuebin)') // true
如今能够经过 description
方法获取 Symbol
的描述:
const name = Symbol('My name is axuebin') console.log(name.description) // My name is axuebin console.log(name.description === 'My name is axuebin') // My name is axuebin
新增的第七种基本数据类型,用来处理超过2
的53
次方之外的数
console.log(11n); // 11n const a = 11n console.log(typeof a) // bigint 是数字,能够进行运算 console.log(typeof 11) // number
PS: 七种基本数据类型:String、Number、Boolean、Null、Undefined、Symbol、BigInt