数组扩展

来源:互联网 发布:淘宝开店进货渠道 编辑:程序博客网 时间:2024/06/16 18:00

类似数组

只要是部署了Iterator接口的数据结构,Array.from都能将其转为数组。

let arr={    '0':'a',    '1':'b',    '2':'c',    length:3}let b=[].slice.call(arr)console.log(b)//['a','b','c']//es6:Array.from(arr)--------------------Array.from('hello')// ['h', 'e', 'l', 'l', 'o']let namesSet = new Set(['a', 'b'])Array.from(namesSet) // ['a', 'b']
  • …扩展运算符
//...运算符将某些数据结构转换成数组[...document.querySelectorAll('div')][...arguments]

Array.from的接受第二个参数相当于map方法

let spans = document.querySelectorAll('span.name');// map()let names1 = Array.prototype.map.call(spans, s => s.textContent);// Array.from()let names2 = Array.from(spans, s => s.textContent)

Array.of()将一组值转成数组

弥补Array()的不足

Array() // []Array(3) // [, , ,]Array(3, 11, 8) // [3, 11, 8]

嵌入Object.is方法

if (!Object.is) {  Object.is = function(x, y) {    // SameValue algorithm    if (x === y) { // Steps 1-5, 7-10      // Steps 6.b-6.e: +0 != -0      return x !== 0 || 1 / x === 1 / y;    } else {     // Step 6.a: NaN == NaN     return x !== x && y !== y;    }  };}console.log(Object.is(0,-0))//falseconsole.log(0==-0)//trueconsole.log(0===-0)//true

find()

数组实例的find()和findIndex()
找出第一个符合条件的

0 0
原创粉丝点击