Answer the question
In order to leave comments, you need to log in
JavaScript array cloning. Why is the browser hanging?
function cloneArray(mas) {
var masClone = new Array();
for(i = 0; i < mas.length; i++) {
masClone.push(mas[i]);
for(j = 0; j < mas[i].length; j++) {
masClone[i].push(mas[i][j]);
}
}
return masClone;
}
var s1 = 0;
var s2 = 1;
var d1 = 1;
var d2 = 0;
var mass1 = [
s = [s1, s2],
d = [d1, d2]
];
var mass2 = cloneArray(mass1);
Answer the question
In order to leave comments, you need to log in
There's some kind of magic that I don't understand...
var mass1 = [
s = [s1, s2],
d = [d1, d2]
];
var isArray = Array.isArray || function (arr) {
return Object.prototype.toString.call(arr) === "[object Array]";
}
var isObject = function (obj) {
return Object.prototype.toString.call(obj) === "[object Object]";
}
function cloneDeep(mas) {
var masClone = isArray(mas) ? new Array(mas.length) : {};
Object.keys(mas).forEach(function (key) {
if (isArray(mas[key]) || isObject(mas[key]))
masClone[key] = cloneDeep(mas[key]);
else
masClone[key] = mas[key];
});
return masClone;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question