Blame view

addons/exam/uniapp/tuniao-ui/libs/function/deepClone.js 613 Bytes
2db47680   hexiaodong   chore: 初始提交 php 遗...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
  /**

   * 判断是否为数组

   * 

   * @param {Object} arr

   */

  function isArray(arr) {

    return Object.prototype.toString.call(arr) === '[object Array]'

  }

  

  /**

   * 深度复制数据

   * 

   * @param {Object} obj

   */

  function deepClone(obj) {

    if ([null, undefined, NaN, false].includes(obj)) return obj

    if (typeof obj !== 'object' && typeof obj !== 'function') {

      return obj

    }

    var o = isArray(obj) ? [] : {}

    for (let i in obj) {

      if (obj.hasOwnProperty(i)) {

        o[i] = typeof obj[i] === 'object' ? deepClone(obj[i]) : obj[i]

      }

    }

    return o

  }

  

  export default deepClone