【补充】数组的过滤

【补充】数组的过滤

  • 数组.filter(匿名函数,接收一个参数,函数必须返回 true/false )
    • 返回 true 则表示该数据保留
var arr = ['a', 'at', 'atom', 'attoo', 'be', 'beyond', 'cs', 'csrf']
    // 数组.filer(匿名函数,接受一个参数,函数必须返回true或false,如果返回true,表示这个值保留)
    var new_arr = arr.filter(function (item) {
        console.log(item)
        if (item.length > 3) {
            return true
        } else {
            return false
        }

    })
    console.log(new_arr)
  • 判断一个字符串是否在另一个字符串中
var s='is'
var s1 = 'lqz is handsome'
var res=s1.indexOf(s)   // s的索引位置,如果大于等于0,说明s在s1中
console.log(res)
  • 过滤出数组中有at的字符串
var arr = ['a', 'at', 'atom', 'attoo', 'be', 'beyond', 'cs', 'csrf']
    var search = 'at'
    var new_arr = arr.filter(function (item) {

        if (item.indexOf(search) >= 0) {
            return true
        } else {
            return false
        }

    })
    console.log(new_arr)
posted @ 2023-08-06 22:27  Chimengmeng  阅读(9)  评论(0编辑  收藏  举报
/* */