1.Array.from用法解析
1) 该类数组对象必须具有length属性,用于指定数组的长度,否则转换后是空数组;
2) 该类数组对象的属性名必须为数值型 或 字符串型的数字;
例:
Array.from({a: 1, b: 2}) //输出 []
Array.from({a: 1, b: 2, length: 3}) //输出 [undefined, undefined, undefined]
Array.from({0: 'a', 1: 'b'}) //输出 []
Array.from({0: 'a', 1: 'b', length: 3}) //输出 ['a', 'b', undefined]
Array.from('hello') //输出 ['h', 'e', 'l', 'l', 'o']
Array.from(new Set([1, 2, 3, 3])) //输出 [1, 2, 3]
Array.from([1, 2, 3], (v, i) => v + 1) //输出 [2, 3, 4]
Array.from(new Array(3), (v, i) => 3) //输出 [3, 3, 3]
2.解构赋值 与 import { test } from '...'
如果是 module.exports = {test: 1, ...},那么可用 import { test } from '...' 或 import { test as testName } from '...' 引入某个单独属性值
当 export {test: 1, ...} 或 export default {test: 1, ...} 时,则用 import 自定义变量名 from '...' 引入整个对象 或 其他值
当export时声明了变量名时,如:export const objName = {test: 1, ...},则用import {objName} from '...' 或 import {objName as otherName} from '...'引入某个同名的变量
小结:用 import { a } from '...' 或 import { a as b } from '...' 这种 单独获取某个属性值 的写法,只能用于module.exports = {...} 或者 export时声明了同名变量a,否则输出
的a只会是undefined。这种写法与作用虽然与解构赋值相似,但实际不是解构赋值,因为解构赋值 不可用 as 关键字
解构赋值:const {test} = {test: 1, test2: 2} //结果:test变量值为 1
解构赋值+扩展运算符:const {a, ...other } = {a: 1, b: 2, c: 3}; //结果:a为 1,other为 {b: 2, c: 3}
3.数组api之find和filter的区别
1) 返回数据结构不同
2) find找到第一个符合条件就终止返回,filter则遍历所有项
[1,2,3,4].find(v => v > 1); // 2
[1,2,3,4].filter(v => v > 1); // [2,3,4]
4.将Map或Set类型数据转换为Array或Object
Array.from(new Set( [1,2,3,3] )) // [1,2,3]
Object.fromEntries(new Map( [ ['a',123] ] )) // { a: 123 }
5.新数据类型:Symbol
Symbol是一种新的原始数据类型,表示独一无二的值,主要用作对象的属性名。对象的属性名现在可以有两种类型,一种是字符串,另一种就是Symbol 类型。凡是属性名属于 Symbol 类型,就都是独一无二的,可以保证不会与其他属性名产生冲突。
用法:
let s1 = Symbol('aaa'); //aaa表示描述该symbol的一个自定义字符串,主要为了转为字符串或控制台输出时,比较容易区分,也可不填
const obj = { aaa: 111, [ s1 ]: 123 };
s1.toString(); // 转为字符串,输出"Symbol(aaa)"
注意:Symbol对象不可以new