1.数据格式化,数字超出三位数 逗号 分割: 123,23

    1. /**
    2. * 格式化数字,超过3个加 逗号 分割
    3. * @returns 123,456
    4. */
    5. function intervalNum(data: number | string) {
    6. if (data === undefined) {
    7. return
    8. }
    9. if (typeof data === 'number') {
    10. data = data.toFixed(2)
    11. }
    12. data = data.replace('.00', '')
    13. data = data.substring(data.length - 2) === '.0' ? data.replace('.0', '') : data
    14. // 小于3位数,直接返回
    15. if (data < '1000') {
    16. return data
    17. }
    18. if (!(data.split('.')[0].length > 3)) {
    19. return data
    20. }
    21. let price = data.split('.')[0]
    22. const pricePoint = data.split('.')[1]
    23. let result = ''
    24. price = price.toString()
    25. result =
    26. price.length > 6
    27. ? `${price.substring(0, price.length - 6)},${price.substring(
    28. price.length - 6,
    29. price.length - 3
    30. )},${price.substring(price.length - 3, price.length)}`
    31. : `${price.substring(0, price.length - 3)},${price.substring(price.length - 3, price.length)}`
    32. result = pricePoint ? `${result},${pricePoint}` : result
    33. return result
    34. }

    2.枚举键值互换

    1. /**
    2. * convertEnum
    3. * 枚举键值互换 title:'你好' => '你好':title
    4. */
    5. export const convertKeyValueEnum = (obj) => {
    6. const result = {}
    7. if (typeof obj === 'object') {
    8. for (const key in obj) {
    9. if (obj.hasOwnProperty(key)) {
    10. result[obj[key]] = key
    11. }
    12. }
    13. }
    14. return result
    15. }