常用的Javascript方法封装

数字金额转换中文大写

function transAmount(value) {
    if ((!value && value !== 0) || !/^(0|[1-9]\d{0,10})(\.\d{0,2})?$/.test(value)) return ''
    const UNIT = ['佰亿', '拾亿', '亿', '仟万', '佰万', '拾万', '万', '仟', '佰', '拾', '元']
    const UNIT1 = ['角', '分']
    const NUMBER = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖']
    let [ integer = '', decimal = '' ] = parseFloat(value).toString().split('.').map(val => {
            return val.split('').map(num => num !== '0' ? NUMBER[Number(num)] : num).join('')
    })
    let integer1 = ''
    UNIT.slice(-integer.length).map((unit, index) => {
        let isZero = integer[index] === '0' && (index !== integer.length - 1) && '0'
        return isZero || `${integer[index]}${unit}`
    }).forEach(item => {
        const key = integer1.replace(/.*([^0])0*/, '$1')
        if (integer1 && item.includes(key)) {
            integer1 = integer1.replace(key, '')
        }
        integer1 += item
    })
    integer = integer1.replace(/(.*[^0])0+([^0])$/, '$1$2').replace(/0+/g, NUMBER[0])
    if (decimal) {
        decimal = UNIT1.map((unit, index) => {
            let isZero = decimal[index] === '0' && '零'
            return isZero || `${decimal[index] ? decimal[index] + unit : ''}`
        }).join('')
        return `${integer}${decimal}`
    } else {
        return `${integer}整`
    }
}

获取数据类型

function getType (value) {
    return Object.prototype.toString.call(value).replace(/\[object (.*)\]/, '$1')
}

数组转树

const arr = [
    { id: 1, name: '类型1', pid: 0 },
    { id: 2, name: '类型2', pid: 1 },
    { id: 3, name: '类型3', pid: 1 },
    { id: 4, name: '类型4', pid: 3 },
    { id: 5, name: '类型5', pid: 4 }
]
function arrayToTree (arr) {
    const result = []
    const childrens = {}
    for (const item of arr) {
        const { pid, id } = item
        childrens[pid] = childrens[pid] || []
        if (pid === 0) {
            result.push(item)
        } else {
            childrens[pid].push(item)
        }
        childrens[id] = childrens[id] || []
        item.childrens = childrens[id]
    }
    return result
 }
// [
//     {
//         "id": 1,
//         "name": "类型1",
//         "pid": 0,
//         "childrens": [
//             {
//                 "id": 2,
//                 "name": "类型2",
//                 "pid": 1,
//                 "childrens": []
//             },
//             {
//                 "id": 3,
//                 "name": "类型3",
//                 "pid": 1,
//                 "childrens": [
//                     {
//                         "id": 4,
//                         "name": "类型4",
//                         "pid": 3,
//                         "childrens": [
//                             {
//                                 "id": 5,
//                                 "name": "类型5",
//                                 "pid": 4,
//                                 "childrens": []
//                             }
//                         ]
//                     }
//                 ]
//             }
//         ]
//     }
// ]
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容