Code Monkey home page Code Monkey logo

collect's Introduction

JS对象、数组封装

  1. objectReplace方法
场景:替换对象键
使用方法:
var obj = {name:'liuning',sex:'man',age:18}
1.普通替换
objectReplace(obj,
    {
        'name':'name1',
        'sex':'xingbie'
    }
)
// {name1:'liuning',xingbie:'man',age:18}
2.复杂替换
objectReplace(obj,
    {
        age:{
           key:'age',
           func: function(age, item){
               return age+'岁'
           }
        }
    }
)
// {name:'liuning',sex:'man',age:'18岁'}
3.新增列
objectReplace(obj,
    {
        desc:{
           func: function(item){
               return `name: ${item.name} age: ${item.age}`
           }
        }
    }
)
// {name:'liuning',sex:'man',age:18,desc:'name liuning age:18'}
  1. objectOnly方法
场景:form表单提交剩余指定项
使用方法:
var obj = {name:'liuning',sex:'man',age:18}
objectOnly(obj, 
    ['name', 'sex']
)
// {name:'liuning',sex:'man'}
  1. objectExcept方法
场景:form表单提交剔除指定项
使用方法:
var obj = {name:'liuning',sex:'man',age:18}
objectExcept(obj, 
    ['age']
)
// {name:'liuning',sex:'man'}
  1. collectReplace方法
场景:列表页格式化日期等
使用方法:同objectReplace
var coll = [
        {
            company:'百度',
            age:12,
            time:'2018-11-21 23:10:10'
        },
        {
            company:'华为',
            age:21,
            time:'2012-11-01 22:11:10'
        },
        {
            company:'腾讯',
            age:14,
            time:'2015-01-11 12:18:09'
        }
    ]
collectReplace(coll, {
    time:{
        key:'created_time',
        fun:function(time){
            return Moment(time).format("YYYY-MM-DD")
        }
    }
})
  1. collectPluck方法
场景:获取筛选指定键组成新的数组
使用方法:
var checkedCitys = [
    {
        name:'申请书',
        code:'1101'
    },
    {
        name:'车辆登记证书',
        code:'3601'
    },
    {
        name:'身份证复印件',
        code:'1106'
    },
]
collectPluck(checkedCitys, 'code')
// ['1101', '3601', '1106']
  1. collectOnly方法
场景:去除多余键
使用方法:同objectOnly
  1. collectExcept方法
场景:去除多余键
使用方法:同objectExcept
  1. collectWhere方法
场景:根据筛选条件获取筛选后的数组
使用方法:
var checkedCitys = [
    {
        name:'申请书',
        code:1101
    },
    {
        name:'车辆登记证书',
        code:3601
    },
    {
        name:'身份证复印件',
        code:1106,
        ss:true
    },
]
1.一个筛选参数
collectWhere(checkedCitys, 'ss')
// [{name:'身份证复印件',code:1106,ss:true}]
2.两个筛选参数
collectWhere(checkedCitys, 'name', '申请书')
// [{name:'申请书',code:1101}]
3.三个筛选参数 支持【==】 【!=】 【>=】 【<=】 暂不支持【===】
collectWhere(checkedCitys, 'name', '!=', '申请书')
// [{name:'车辆登记证书',code:3601},{name:'身份证复印件',code:1106,ss:true}]
4.传入方法 使用方法同filter
collectWhere(checkedCitys, function(item,index){
    return item.code==1101
})
  1. collectFirst方法
场景:根据筛选条件获取筛选后的数组的第一项 无内容返回false
使用方法:
var checkedCitys = [
    {
        name:'申请书',
        code:1101
    },
    {
        name:'车辆登记证书',
        code:3601
    },
    {
        name:'身份证复印件',
        code:1106,
        ss:true
    },
]
1.无筛选参数
collectFirst(checkedCitys)
// {name:'申请书',code:1101}
2.一个筛选参数
collectFirst(checkedCitys, 'ss')
// {name:'身份证复印件',code:1106, ss:true}
3.两个筛选参数
collectFirst(checkedCitys, 'name', '申请书')
// {name:'申请书',code:1101}
4.三个筛选参数 支持【==】 【!=】 【>=】 【<=】 暂不支持【===】
collectFirst(checkedCitys, 'name', '!=', '申请书')
// {name:'车辆登记证书',code:3601}
5.传入方法 使用方法同filter
collectWhere(checkedCitys, function(item,index){
    return item.code==1101
})
  1. collectLast方法
场景:根据筛选条件获取筛选后的数组的最后一项 无内容返回false
使用方法 同collectFirst

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.