数组扩展

类似数组

只要是部署了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))//false
console.log(0==-0)//true
console.log(0===-0)//true

find()

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值