码迷,mamicode.com
首页 > 编程语言 > 详细

ES6 之数值扩展和数组扩展

时间:2019-12-28 13:16:55      阅读:91      评论:0      收藏:0      [点我收藏+]

标签:sign   value   元素   spl   数组   abc   OLE   init   with   

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

// 数值扩展:
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
  [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‘]
}

  

ES6 之数值扩展和数组扩展

标签:sign   value   元素   spl   数组   abc   OLE   init   with   

原文地址:https://www.cnblogs.com/lk-food/p/12111306.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!