Array.apply(null, { length: 5 })

https://developer.mozilla.org...

图片描述

注意:ES5,apply函数的第二个参数除了可以是数组外,还可以是类数组对象

Array.apply(null, { length: 5 })
// 结果 [undefined, undefined, undefined, undefined, undefined]

Array(5)
//结果 [empty × 5] => [,,,,]

为什么要这么写?

map函数并不会遍历数组中没有初始化或者被delete的元素(有相同限制还有forEach, reduce方法)。
Array.apply(null, { length: 5 }) 是用来初始化一个长度为5,每项的初始值都是undefined的数组

 render (createElement) {
      return createElement('div',
        Array.apply(null, { length: 20 }).map(function () {
          return createElement('p', 'hi')
        })
      )
    }

还有种用法:

图片描述

Array.apply(null, {length: N}).map(Number.call, Number)为range的hack;

keys.forEach((i, idx) => {
          d3.select(this).append('rect')
            .attr('x', x(d.date) - bw / 2)
            .attr('y', gH -
                  // Array.apply(null, {length: N}).map(Number.call, Number)为range的hack;idx + 1保证reduce结果正确(1对应[1], 2对应[1,2])
                  Array.apply(null, {length: idx + 1}).map(Number.call, Number)
                       .reduce((p, n, j) => p + y(d[keys[j]]), 0)
            )
            .attr('height', y(d[keys[idx]]))
            .attr('width', bw)
            .attr('fill', color)
            .attr('opacity', 1 - idx * 0.3)
        })

 deepclone

// detail: https://www.zhihu.com/question/23031215
// about deepCopy: http://jerryzou.com/posts/dive-into-deep-clone-in-javascript/
export function deepClone (obj) {
  let str
  let newobj = obj.constructor === Array ? [] : {}
  if (typeof obj !== 'object') {
    return
  } else if (window.JSON) {
    str = JSON.stringify(obj) // 系列化对象
    newobj = JSON.parse(str) // 还原
  } else {
    for (let i in obj) {
      newobj[i] = typeof obj[i] === 'object' ? deepClone(obj[i]) : obj[i]
    }
  }
  return newobj
}

千位分割符号

// 千位分隔符
export function thousandSplit (val) {
  if (typeof val !== 'number') return
  let tempArr = String(val).split('.')
  let strArr = tempArr[0].split('')
  let result = ''
  for (let i = strArr.length - 1, j = 0; i >= 0; i--, j++) {
    result = strArr[i] + result
    if (j % 3 === 2 && i !== 0) result = ',' + result
  }
  if (tempArr.length > 1) result += '.' + tempArr[1]
  return result
}
Number(string).tolocaleString()
// 金额按千位逗号拆分,判断是不是带小数,带小数的话如果大于6位就不展示,否则展示小数
const toThousands = (num2) => {
  // 判断是不是num类型
  if (!isNaN(num2)) {
    let r = /^[0-9]*[1-9][0-9]*$/  //正整数
    // 首先判断是不是整数
    if (!r.test(Math.abs(num2)) && parseInt(num2) !== 0) {
      // 如果不是整数且小于7位数
      if (Math.abs(parseInt(num2)).toString().length <= 6) {
        num2 = num2.split('.')
        let num = ((num2[0] < 0 ? Math.abs(num2[0]) : num2[0]) || 0).toString(), result = '';
        while (num.length > 3) {
            result = ',' + num.slice(-3) + result;
            num = num.slice(0, num.length - 3);
        }
        result = num ? (num + result) : ''
        if (num2[0] < 0) {
          return '-' + result + '.' + num2[1];
        } else if (num2[0] >= 0) {
          return result + '.' + num2[1]
        }
      } else {
        num2 = parseInt(num2)
        let num = ((num2 < 0 ? Math.abs(num2) : num2) || 0).toString(), result = '';
        while (num.length > 3) {
            result = ',' + num.slice(-3) + result;
            num = num.slice(0, num.length - 3);
        }
        result = num ? (num + result) : ''
        if (num2 < 0) {
          return '-' + result;
        } else if (num2 >= 0) {
          return result
        }
      }
    } else {
      num2 = parseInt(num2)
      let num = ((num2 < 0 ? Math.abs(num2) : num2) || 0).toString(), result = '';
      while (num.length > 3) {
          result = ',' + num.slice(-3) + result;
          num = num.slice(0, num.length - 3);
      }
      result = num ? (num + result) : ''
      if (num2 < 0) {
        return '-' + result;
      } else if (num2 >= 0) {
        return result
      }
    }
  } else {
    return num2
  }
}

参考

Ramda 函数库参考教程


白鲸鱼
1k 声望110 粉丝

方寸湛蓝