UNPKG

xxm-test-js

Version:
34 lines 999 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.deepCopy = deepCopy; /** * 创建一个对象或数组的深拷贝。 * Copyright (c) 2024 xxm * * @param obj - 需要被拷贝的对象或数组。 * @returns 一个新的对象或数组,其属性和值与原始对象相同。 * * @example * ```js * const originalObj = { a: 1, b: { c: 2 } }; * const copiedObj = deepCopy(originalObj); * console.log(copiedObj); // 输出: { a: 1, b: { c: 2 } } * * const originalArr = [1, 2, [3, 4]]; * const copiedArr = deepCopy(originalArr); * console.log(copiedArr); // 输出: [1, 2, [3, 4]] * ``` */ function deepCopy(obj) { if (obj === null || typeof obj !== 'object') { return obj; } const copy = Array.isArray(obj) ? [] : {}; for (const key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { copy[key] = deepCopy(obj[key]); } } return copy; } //# sourceMappingURL=deepCopy.js.map