参考文献数组
这个方法比较有意思,能够将一个相似数组或可迭代对象分割,而后将值返回为一个新数组app
伪数组对象(拥有一个 length
属性和若干索引属性的任意对象)ide
可迭代对象](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Guide/iterable)(能够获取对象中的元素,如 Map和 Set 等)函数
这个方法有三个参数,第一个就是要转换成数组的伪数组或者可迭代对象,第二个参数就是至关于一个map方法,第三个参数就是map函数执行时里面的的this
指向对象,有点相似bind
,call
,apply
,能够将数据和处理数据的对象方法分开ui
const DObj = { handleArr: function (x) { return x+'1' } } Array.from('Hello World !',function (v) { return this.handleArr(v)}, DObj) // 打印结果 ["H1", "e1", "l1", "l1", "o1", " 1", "W1", "o1", "r1", "l1", "d1", " 1", "!1"]
Tips 使用第三个参数时,map函数方法不要使用箭头函数,不然this指向不会改变,若是不涉及第三个参数,可使用箭头函数this
Mapcode
let m = new Map() m.set('one', 'H') m.set('tow', 'a') m.set('three', 'a') const DObj = { handleArr: function (x) { return x + '1' } } newData = Array.from(m, function (v) { return this.handleArr(v)}, DObj) console.log( newData) // 打印结果 ["one,H1", "tow,a1", "three,a1"]
Set对象
let m = new Set() m.add('H') m.add('a') m.add('o') const DObj = { handleArr: function (x) { return x + '1' } } newData = Array.from(m, function (v) { return this.handleArr(v)}, DObj) console.log( newData) // 打印结果 ["H1", "a1", "o1"]
类数组对象索引
只要对象的key
是数值,而且value
不能为数字,就能够视做类数组对象,key
就是索引three
这个方法是用来检测某个数据或者变量是不是数组,若是是则返回true
,不然返回false
const arr = [1,2,3,4,5]; const obj = {name: '李狗蛋'}; const str = 'Hello World'; console.log(Array.isArray(arr)) console.log(Array.isArray(str)) console.log(Array.isArray(obj)) // 打印结果 true false false
Tips typeof
用来检测数据或变量的类型,可是只是返回个类型,并不精准,instanceof
用来检测数据或者变量是否某个对象的实例,可是数组也会返回true
这个放个和Array构造函数有点相似,均可以根据里面的参数建立数组,不过不一样的是:
1.of
方法是把里面的每个参数做为数组的项,若是只是一个数字参数,依旧也是一个数组的项
2.Array
构造函数会把里面的每个参数做为数组的项,若是只是一个数字参数,则会建立一个该长度的空数组