var test = {
a: [1,2,3],
b: 'sfasdf',
c: {
d: 1,
e: true,
f: ['a', 'b', 'c']
},
d: null,
}
function clone(val) {
if(typeof val === 'number' || typeof val ==='string' || typeof val === 'boolean' || val === null) {
return val;
}
if(Object.prototype.toString.call(val) === '[object Array]') {
return val.map(v=>clone(v));
}
if(Object.prototype.toString.call(val) === "[object Object]") {
return Object.keys(val).reduce((prev, key) => {
prev[key] = clone(val[key]);
return prev;
}, {});
}
}
const newTest = clone(test);
console.log(newTest);
console