I am trying to implement a clone function but I am not sure if I am doing it right while trying to clone '[object Function]'
. You will see the result at the bottom. I am not sure if desired result should look like the original input data. Let me know what you think and if you have any ideas on how to implement it. Here is the code.
UPD: actually it works as it supposed to be working. I am going to leave it here so people can use it if they have the same question.
function deep(value) {
if (typeof value !== 'object' || value === null) {
return value;
}
if (Array.isArray(value)) {
return deepArray(value);
}
return deepObject(value);
}
function deepObject(source) {
const result = {};
Object.keys(source).forEach(key => {
const value = source[key];
result[key] = deep(value);
});
return result;
}
function deepArray(collection) {
return collection.map(value => {
return deep(value);
});
}
const id1 = Symbol('id');
const value = {
a: 2,
f: id1,
b: '2',
c: false,
g: [
{ a: { j: undefined }, func: () => {} },
{ a: 2, b: '2', c: false, g: [{ a: { j: undefined }, func: () => {} }] }
]
};
RESULT
{ a: 2,
f: Symbol(id),
b: '2',
c: false,
g:
[ { a: { j: undefined }, func: [Function: func] },
{ a: 2,
b: '2',
c: false,
g: [ { a: { j: undefined }, func: [Function: func] } ] } ] }