• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
Rgzs
博客园    首页    新随笔    联系   管理    订阅  订阅
es6 数组操作

1、Array.of()  将任意一组值转换为数组

//Array.of()
    // var arr = new Array(1, 2, 3, 5, 8)
    // var str = '12,3,5,6,ss'
    // console.log(Array.of(str))

2、Array.from()  将类(伪)数组转换为数组

<body>
    <ul>
        <li>1</li>
        <li>2</li>
        <li>3</li>
        <li>4</li>
        <li>5</li>
        <li>6</li>
        <li>7</li>
        <li>8</li>
    </ul>
</body>
// //Array.from()
    // let list = document.getElementsByTagName('li')
    // var ul = Array.from(list)
    // console.log(ul)

3、find() 找出数组中符合条件的第一个元素

    // 注意:未找到返回undefined
 let arr = [1, 2, 3,4, 5, 6]
    console.log(arr.find(function (value) {
            return value > 7;  
        }))      //undefined

4、findIndex() 找出数组中符合条件的第一个元素位置

    // 未找到返回-1 

 let arr = [1, 2, 3,4, 5, 6]
console.log(arr.findIndex(function(value){
        return value>8;
    }))  //-1

5、fill() 填充数组中的每一项或者某一项

    // fill(x) x是填充内容
    // fill(x,y,z) x是填充内容,y是起始的索引值,z是结束的索引值,不包括该值    
会改变原数组

  

//fill  会改变原数组
    let arr1 = [1,2,3,5,9,4]
    console.log(arr1.fill(3))   //[3,3,3,3,3,3]
    console.log(arr1)       //[3,3,3,3,3,3]
console.log(arr1.fill(4,2,4))   //[3,3,4,4,3,3]
console.log(arr1)

6、entries()  遍历器返回的键值对k:v    (索引):(属性值)

//entries  
    for(let[i,v]of ['a','b'].entries())
    {
        console.log(i,v)
    }    // 0 a
1 b

7、 keys() 返回键

//keys  索引
    for (let index of ['a', 'b'].keys()) {
            console.log(index)
        }  // 0  1

8、values()  返回值

// values  值
    for (let value of ['a', 'b'].values()) {
            console.log(value)  
        }   // a   b

 

posted on 2020-09-01 21:11  飄落的葉子  阅读(210)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3