JS 深度复制数组

/**
 * 深度复制数组
 * @param data
 * @returns {{}}
 */
function deepClone(data) {
    let d;
    if (typeof data === "object") {
        if (data == null) {
            d = null;
        } else {
            if (data.constructor === Array) {
                d = [];
                for (let i in data) {
                    d.push(deepClone(data[i]));
                }
            } else {
                d = {};
                for (let i in data) {
                    d[i] = deepClone(data[i]);
                }
            }
        }
    } else {
        d = data;
    }
    return d;
}

 

posted @ 2021-12-22 15:45  卡农的忧伤ろ◆  阅读(73)  评论(0)    收藏  举报