创建一个新数组,将array与任何数组或值连接在一起

本文介绍了一个JavaScript函数concat,该函数可以将一个数组与其他数组或任意值进行拼接,并支持扁平化操作。文章详细解释了如何通过递归方式实现数组的扁平化,同时确保了原始数组不会被修改。

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

/**
 * 创建一个新数组,将array与任何数组或值连接在一起
 * Creates a new array concatenating `array` with any additional arrays and/or values
 *
 * @param {Array} array The array to concatenate
 * @param {...*} [values] The values to concatenate
 * @returns {Array} Returns the new concatenated array
 * @example
 *
 * var array = [1];
 * var other = concat(array, 2, [3], [[4]]);
 *
 * console.log(other)
 * // => [1, 2, 3, [4]]
 * console.log(arrat)
 * // => [1]
 */

//内置的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
}

// Copies the values of `source` to `array`
function copyArray(source, array) {
  var index = -1,
    length = source.length

  array || (array = Array(length))
  while (++index < length) {
    array[index] = source[index]
  }
  return array
}

function isObjectLike(value) {
  return typeof value == "object" && value !== null;
}

// 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) {
        baseFlatten(array, depth - 1, predicate, isStrict, result)
      } else {
        arrayPush(result, value)
      }
    } else if (!isStrict) {
      result[result.length] = value
    }
  }
  return result
}

function concat() {
  var length = arguments.length
  if (!length) {
    return []
  }
  var args = Array(length - 1),
    array = arguments[0],
    index = length

  while (index--) {
    args[index - 1] = arguments[index]
  }
  return arrayPush(
    Array.isArray(array) ? copyArray(array) : [array],
    baseFlatten(args, 1)
  )
}

export default concat

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值