zoukankan      html  css  js  c++  java
  • ES6使用的一些方法

    查找数组中符合条件的所有记录

    var list=[
    {id:1,name:"张三"},
    {id:2,name:"李四"},
    {id:3,name:"王五"},
    {id:4,name:"张六"}
    ]

    有以上数组,查找名字中有“张”的对象,在以前我们的写法:

    //先定义一个空数组,然后for循环查找符合条件的记录添加
    var newlist=[];
    for(var i=0;i<list.count;i++){
        if(list[i].name.indexOf('张')>-1){
            newlist.push(list[i])
        }
    }    
    有了ES6之后,我们可以这样写:

    filter方法:
    //ES6的数组新方法 filter
    list.filter(function(item){ if(item.name.indexOf("searchStr")>-1){   return item   } })

    然后可以使用同样是ES6的箭头函数和includes方法:

    includes(string)方法:如果包含了该字符串则返回true

    list.filter(item=>{
        if(item.name.includes("张")){
           return item
        }
    })

     查找数组并返回索引,只能返回一个:findIndex

    var index=list.findIndex(item=>{
        if(item.name.includes('张')){
            return ture;
        }
    })
  • 相关阅读:
    xshell 缺少mfc110u.dll
    notepad++ 常用插件
    java 发送 http post 和 get 请求(利用unirest)
    my.conf配置大全
    md5算法
    sprinboot+redis
    jq下拉插件,chosen
    springboot+idea 热部署
    Jquery 监听浏览器前进后退
    手机自带的表情入库
  • 原文地址:https://www.cnblogs.com/marisen/p/10493285.html
Copyright © 2011-2022 走看看