根据depth递归减少array的嵌套层级

本文探讨如何通过递归方法有效减少数组的嵌套层级,提高数据处理效率,适用于处理大量深度嵌套的数据结构。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/**
 * 根据depth递归减少array的嵌套层级
 * Recursively flatten `array` up to `depth` times.
 * @param {Array} The array to flatten
 * @param {number} [depth=1] The maximum recursion depth.
 * @returns {Array} Returns the new flattened array.
 * @example
 *
 * var array = [1, [2, [3, [4]], 5]]
 * flattenDepth(array, 1)
 * // =>  [1, 2, [3, [4]], 5]
 * flattenDepth(array, 2)
 * // => [1, 2, 3, [4], 5]
 */

const NAN = 0 / 0
const INFINITY = 1 / 0
const symbolTag = "[object Symbol]"
const MAX_INTEGER = Number.MAX_VALUE || 1.7976931348623157e308
 
function isObjectLike(value) {
  return typeof value == "object" && value !== null
}
 
function isSymbol(value) {
  return (
    typeof value === "symbol" ||
    (isObjectLike(value) && Object.prototype.toString.call(value) === symbolTag)
  )
}
 
function toNumber(value) {
  if (typeof value === "number") {
    return value
  }
  if (isSymbol(value)) {
    return NAN
  }
  return Number(value)
}

function toFinite(value) {
  if (!value) {
    return value === 0 ? value : 0
  }
  value = toNumber(value)
  if (value === INFINITY || value === -INFINITY) {
    var sign = value < 0 ? -1 : 1 //可用Math.sign代替
    return sign * MAX_INTEGER
  }
  return value === value ? value : 0 //NaN 不等于 NaN
}

function toInteger(value) {
  var result = toFinite(value)
  var remainder = result % 1
  return result === result ? (remainder ? result - remainder : result) : 0 //可用Math.trunc代替
}

//内置的Symbol.isConcatSpreadable符号用于配置某对象作为Array.prototype.concat()方法的参数时是否展开其数组元素
const spreadableSymbol = Symbol ? Symbol.isConcatSpreadable : undefined

// Appends the elements of `value` to `array`
function arrayPush(array, values) {
  var index = -1,
    length = values.length,
    offset = array.length

  while (++index < length) {
    array[offset + index] = values[index]
  }
  return array
}

// Checks if `value` is likely an `arguments` object

function baseIsArguments(value) {
  return isObjectLike(value) && Object.prototype.toString.call(value) === argsTag
}

const isArguments = baseIsArguments(
  (function() {
    return arguments
  })()
)
  ? baseIsArguments
  : function(value) {
      return (
        isObjectLike(value) &&
        Object.prototype.hasOwnProperty.call(value, "callee") &&
        !Object.prototype.propertyIsEnumerable.call(value, "callee")
      )
    }

// Checks if `value` is a flattenable `arguments` object or array
function isFlattenable(value) {
  return (
    Array.isArray(value) ||
    isArguments(value) ||
    !!(spreadableSymbol && value && value[spreadableSymbol])
  )
}

// Flattens `array` a single level deep.
function baseFlatten(array, depth, predicate, isStrict, result) {
  var index = -1,
    length = array.length

  predicate || (predicate = isFlattenable)
  result || (result = [])

  while (++index < length) {
    var value = array[index]
    if (depth > 0 && predicate(value)) {
      if (depth > 1) {
        // Recursively flatten arrays
        baseFlatten(value, depth - 1, predicate, isStrict, result)
      } else {
        arrayPush(result, value)
      }
    } else if (!isStrict) {
      result[result.length] = value
    }
  }
  return result
}

function flattenDepth(array, depth) {
  var length = array == null ? 0 : array.length
  if (!length) {
    return []
  }
  depth = depth === undefined ? 1 : toInteger(depth)
  return baseFlatten(array, depth)
}

export default flattenDepth

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值