Skip to main content
deleted 6 characters in body
Source Link
marc_s
  • 759.6k
  • 185
  • 1.4k
  • 1.5k

For the arrays and objects which contain array/objects with primitives as fields values is possible to use JSON.parse(JSON.stringify(structure)) as posted many times here.

but for modern fields values as React components it will not work, because JSON.parse convertationconversion just ruin sensitive content of React object which contains many special characters.

In this case good to use lodash/cloneDeep (not _.cloneDeep, because it's spoiling tree shaking)

For the arrays and objects which contain array/objects with primitives as fields values is possible to use JSON.parse(JSON.stringify(structure)) as posted many times here.

but for modern fields values as React components it will not work, because JSON.parse convertation just ruin sensitive content of React object which contains many special characters.

In this case good to use lodash/cloneDeep (not _.cloneDeep, because it's spoiling tree shaking)

For the arrays and objects which contain array/objects with primitives as fields values is possible to use JSON.parse(JSON.stringify(structure)) as posted many times here.

but for modern fields values as React components it will not work, because JSON.parse conversion just ruin sensitive content of React object which contains many special characters.

In this case good to use lodash/cloneDeep (not _.cloneDeep, because it's spoiling tree shaking)

deleted 1472 characters in body
Source Link

For the arrays and objects which contain array/objects with primitives as fields values is possible to use JSON.parse(JSON.stringify(structure)) as posted many times here.

but for modern fields values as React components it will not work, because JSON.parse convertation just ruin sensitive content of React object which contains many special characters.

In this case good to use lodash/cloneDeep (not _.cloneDeep, because it's spoiling tree shaking)

or try this

const structureArray = [1, 2, [3, 4], 5, [6, 7, [8, 9]]];
const structureObject = {
    a: 1,
    b: 2,
    c: {
        d: 3,
        e: 4,
        f: {
            g: 5,
            h: [6, 8, 9]
        }
    }
};
const deepClone = (structure) => {
    if (!structure || typeof structure !== 'object') return structure;

    const isArray = Array.isArray(structure);

    const source = isArray ? structure : Object.entries(structure)

    return source.reduce((acc, item) => {
        if (isArray) {
            if (Array.isArray(item)) {
                acc.push(deepClone(item));
            } else {
                acc.push(item);
            }
        } else {
            const [key, value] = item;
            if (typeof value === 'object') {
                acc[key] = deepClone(value);
            } else {
                acc[key] = value;
            }
        }
        return acc;

    }, isArray ? [] : {});

};

console.log(deepClone(structureArray));
console.log(structureArray === deepClone(structureArray));
console.log(deepClone(structureObject));
console.log(structureArray === deepClone(structureObject));

For the arrays and objects which contain array/objects with primitives as fields values is possible to use JSON.parse(JSON.stringify(structure)) as posted many times here.

but for modern fields values as React components it will not work, because JSON.parse convertation just ruin sensitive content of React object which contains many special characters.

In this case good to use lodash/cloneDeep (not _.cloneDeep, because it's spoiling tree shaking)

or try this

const structureArray = [1, 2, [3, 4], 5, [6, 7, [8, 9]]];
const structureObject = {
    a: 1,
    b: 2,
    c: {
        d: 3,
        e: 4,
        f: {
            g: 5,
            h: [6, 8, 9]
        }
    }
};
const deepClone = (structure) => {
    if (!structure || typeof structure !== 'object') return structure;

    const isArray = Array.isArray(structure);

    const source = isArray ? structure : Object.entries(structure)

    return source.reduce((acc, item) => {
        if (isArray) {
            if (Array.isArray(item)) {
                acc.push(deepClone(item));
            } else {
                acc.push(item);
            }
        } else {
            const [key, value] = item;
            if (typeof value === 'object') {
                acc[key] = deepClone(value);
            } else {
                acc[key] = value;
            }
        }
        return acc;

    }, isArray ? [] : {});

};

console.log(deepClone(structureArray));
console.log(structureArray === deepClone(structureArray));
console.log(deepClone(structureObject));
console.log(structureArray === deepClone(structureObject));

For the arrays and objects which contain array/objects with primitives as fields values is possible to use JSON.parse(JSON.stringify(structure)) as posted many times here.

but for modern fields values as React components it will not work, because JSON.parse convertation just ruin sensitive content of React object which contains many special characters.

In this case good to use lodash/cloneDeep (not _.cloneDeep, because it's spoiling tree shaking)

Source Link

For the arrays and objects which contain array/objects with primitives as fields values is possible to use JSON.parse(JSON.stringify(structure)) as posted many times here.

but for modern fields values as React components it will not work, because JSON.parse convertation just ruin sensitive content of React object which contains many special characters.

In this case good to use lodash/cloneDeep (not _.cloneDeep, because it's spoiling tree shaking)

or try this

const structureArray = [1, 2, [3, 4], 5, [6, 7, [8, 9]]];
const structureObject = {
    a: 1,
    b: 2,
    c: {
        d: 3,
        e: 4,
        f: {
            g: 5,
            h: [6, 8, 9]
        }
    }
};
const deepClone = (structure) => {
    if (!structure || typeof structure !== 'object') return structure;

    const isArray = Array.isArray(structure);

    const source = isArray ? structure : Object.entries(structure)

    return source.reduce((acc, item) => {
        if (isArray) {
            if (Array.isArray(item)) {
                acc.push(deepClone(item));
            } else {
                acc.push(item);
            }
        } else {
            const [key, value] = item;
            if (typeof value === 'object') {
                acc[key] = deepClone(value);
            } else {
                acc[key] = value;
            }
        }
        return acc;

    }, isArray ? [] : {});

};

console.log(deepClone(structureArray));
console.log(structureArray === deepClone(structureArray));
console.log(deepClone(structureObject));
console.log(structureArray === deepClone(structureObject));