ES6 之数值扩展和数组扩展

单纯记录,方便快速复习。

// 数值扩展:
console.log(  //有穷、整数、非数字判断
Number.isFinite(10),
Number.isInteger(12),
Number.isInteger(12.1),
Number.isNaN(NaN),
Number.isNaN(10)
)
console.log( //判断正数、负数、0
  Math.sign(2),
  Math.sign(0),
  Math.sign(-1),
  Math.sign('2'), // 1
  Math.sign('abc')  //NaN
)
console.log( //获取小数的 整数部分
  Math.trunc(1.2),
  Math.trunc(4.2)  // 4
)

// 数组扩展
console.log(
  Array.of(1,2,3,4), // [1, 2, 3, 4]
  Array.of(),   // []
  Array.from('a'),  //集合转换成数组
  Array.from("http1,http2,http3",item => item.split(',')),
  Array.from([1,2,3],(item)=>item*2) // 接收两个参数,函数对数组进行map动作
)
console.log(
  [1,2,undefined].fill(7),// [7,7,7] 全部替换
  ['a','b','c','d','e'].fill(1,0,3),  // 把从 0 位置到 3 位置元素,替换为 1
 注意 splice(idx,howmany,item1,item2,...) 方法同样可以实现,参数含义为 从什么位置开始删,删除几个,替换的元素是什么
 fill(item,indexstart,indexend) // 替换元素是什么,开始替换位置,结束替换位置(不含结束位置元素)
 var arr = [1,2,3,4]
 arr.fill('a',0,2) // arr ['a','a',3,4]
arr.splice(0,2,'a','a') //arr ['a','a',3,4]
[1,2,3,4,5,6,7,8].fill(0,1,2) ) { // keys、values、entries for(let idx in ['a','b','c'].keys()) { console.log('idx',idx) } for(let value in ['a','b','c'].values()) { console.log('value',value) } for(let [index,value] in ['a','b','c'].entries()) { console.log(` index: ${index}, value: ${value} `) } } { // find findIndex includes console.log( [1,2,3,4].find(item => item > 2) ) console.log( [1,2,3,4].findIndex( item => item>2 ) ) console.log( [1,2,3,4].includes(2) ) console.log( [1,2,3,NaN].includes(NaN) ) //true } { // 被替换的是下标为 0 的元素,用来替换的是下标 2(包含2)到下标3之间的元素 console.log( [1,2,3,'a'].copyWithin(0,2,3)) // [3,2,3,'a'] }

  

posted @ 2019-12-28 12:23  XLLANG  阅读(366)  评论(0编辑  收藏  举报