有一个 javascript 循环引用问题
having a javascript circular reference issue
我有一个函数 createminor4(arr, tourney)
它基本上将 arr 分成 4 组,每组 8 个,并一次将它们交换到 tourney 1 组。
它从那里推入四个{},其中有 4 个键和空数组。
我在 chrome 中逐步完成了它,在第一组正确推送后,下一组将覆盖前一组,即使我推送到四个{}中的不同键也是如此。
我最终得到了一个循环引用,不明白我做错了什么。
*createMinor4() 应该 return:
four {
"A":[["Lava Lamps", ..."shooters"],["a", "b"],["c", "d"],["e", "f"],["g"]],
"B":[["Yellow Dragons", ... "water pistols"],["a", "b"],["c", "d"],["e", "f"],["g"]],
"C":[["Snakes", ... "Hawks"],["a", "b"],["c", "d"],["e", "f"],["g"]],
"D":[["Gamers", ... "worms"],["a", "b"],["c", "d"],["e", "f"],["g"]]}
let players4 = [
"Lava Lamps", "Jarheads", "Cloud Gazerz", "Cobras", "Red Ravens", "Leaders", "Destroyers", "shooters",
"Yellow Dragons", "Whales", "fearwolves", "Hitters", "red rebels", "Knights", "cute gremlins", "water pistols",
"Snakes", "Ravens", "Chariots-of-Hell", "Predators", "Assassins", "Gladiators", "Broncos", "Hawks",
"Gamers", "Slickers", "Freeze", "Rabbits", "Holy Rollers", "Crusaders", "FireHawks", "Worms"
]
let minorTourny = [
["t1", "t2"],
["t3", "t4"],
["t5", "t6"],
["t7", "t8"],
["a", "b"],
["c", "d"],
["e", "f"],
["g"]
];
function createMinor4(arr, tourney) {
let q = arr.length * .25;
let s = 0;
let p = 1;
let g = 4;
let x = 0;
let y = 0;
let z = 0;
let four = {
"A": [],
"B": [],
"C": [],
"D": []
}
//while 4 > 0
while (g > s) {
do {
let loadTeam = tourney[x].splice(y, 1, arr[z]);
if (z === 0) {
loadTeam;
y++;
} else if (z % 2 == 0) {
loadTeam;
y = 1;
} else if (z % 2 !== 0) {
loadTeam;
y = 0;
x++;
}
z++;
}
while (z < q) // while 0 < 8
if (g === 4) {
four.A.push(tourney);
} else if (g === 3) {
four.B.push(tourney);
} else if (g === 2) {
four.C.push(tourney);
} else if (g === 1) {
four.D.push(tourney);
}
arr.splice(0, q)
g--;
p++;
z = 0;
x = 0;
y = 0;
}
return four
}
console.log(createMinor4(players4, minorTourny))
当您在 Array 上使用 splice
时,它也会更改原始 Array 这就是为什么 players4
和 minorTourny
在 createMinor4
函数调用后不相同的原因
您可以使用 Deep copy
当您使用深拷贝时,它将不再引用原始数组
What is the most efficient way to deep clone an object in JavaScript?
let players4 = [
"Lava Lamps", "Jarheads", "Cloud Gazerz", "Cobras", "Red Ravens", "Leaders", "Destroyers", "shooters",
"Yellow Dragons", "Whales", "fearwolves", "Hitters", "red rebels", "Knights", "cute gremlins", "water pistols",
"Snakes", "Ravens", "Chariots-of-Hell", "Predators", "Assassins", "Gladiators", "Broncos", "Hawks",
"Gamers", "Slickers", "Freeze", "Rabbits", "Holy Rollers", "Crusaders", "FireHawks", "Worms"
]
let minorTourny = [
["t1", "t2"],
["t3", "t4"],
["t5", "t6"],
["t7", "t8"],
["a", "b"],
["c", "d"],
["e", "f"],
["g"]
];
function createMinor4(_arr, _tourney) {
const arr = JSON.parse(JSON.stringify(_arr)); // Use Deep copy
const tourney = JSON.parse(JSON.stringify(_tourney)); // Use Deep copy
let q = arr.length * .25;
let s = 0;
let p = 1;
let g = 4;
let x = 0;
let y = 0;
let z = 0;
let four = {
"A": [],
"B": [],
"C": [],
"D": []
}
//while 4 > 0
while (g > s) {
do {
let loadTeam = tourney[x].splice(y, 1, arr[z]);
if (z === 0) {
loadTeam;
y++;
} else if (z % 2 == 0) {
loadTeam;
y = 1;
} else if (z % 2 !== 0) {
loadTeam;
y = 0;
x++;
}
z++;
}
while (z < q) // while 0 < 8
if (g === 4) {
four.A.push(tourney);
} else if (g === 3) {
four.B.push(tourney);
} else if (g === 2) {
four.C.push(tourney);
} else if (g === 1) {
four.D.push(tourney);
}
arr.splice(0, q)
g--;
p++;
z = 0;
x = 0;
y = 0;
}
return four
}
console.log(createMinor4(players4, minorTourny))
我有一个函数 createminor4(arr, tourney) 它基本上将 arr 分成 4 组,每组 8 个,并一次将它们交换到 tourney 1 组。 它从那里推入四个{},其中有 4 个键和空数组。
我在 chrome 中逐步完成了它,在第一组正确推送后,下一组将覆盖前一组,即使我推送到四个{}中的不同键也是如此。 我最终得到了一个循环引用,不明白我做错了什么。
*createMinor4() 应该 return:
four {
"A":[["Lava Lamps", ..."shooters"],["a", "b"],["c", "d"],["e", "f"],["g"]],
"B":[["Yellow Dragons", ... "water pistols"],["a", "b"],["c", "d"],["e", "f"],["g"]],
"C":[["Snakes", ... "Hawks"],["a", "b"],["c", "d"],["e", "f"],["g"]],
"D":[["Gamers", ... "worms"],["a", "b"],["c", "d"],["e", "f"],["g"]]}
let players4 = [
"Lava Lamps", "Jarheads", "Cloud Gazerz", "Cobras", "Red Ravens", "Leaders", "Destroyers", "shooters",
"Yellow Dragons", "Whales", "fearwolves", "Hitters", "red rebels", "Knights", "cute gremlins", "water pistols",
"Snakes", "Ravens", "Chariots-of-Hell", "Predators", "Assassins", "Gladiators", "Broncos", "Hawks",
"Gamers", "Slickers", "Freeze", "Rabbits", "Holy Rollers", "Crusaders", "FireHawks", "Worms"
]
let minorTourny = [
["t1", "t2"],
["t3", "t4"],
["t5", "t6"],
["t7", "t8"],
["a", "b"],
["c", "d"],
["e", "f"],
["g"]
];
function createMinor4(arr, tourney) {
let q = arr.length * .25;
let s = 0;
let p = 1;
let g = 4;
let x = 0;
let y = 0;
let z = 0;
let four = {
"A": [],
"B": [],
"C": [],
"D": []
}
//while 4 > 0
while (g > s) {
do {
let loadTeam = tourney[x].splice(y, 1, arr[z]);
if (z === 0) {
loadTeam;
y++;
} else if (z % 2 == 0) {
loadTeam;
y = 1;
} else if (z % 2 !== 0) {
loadTeam;
y = 0;
x++;
}
z++;
}
while (z < q) // while 0 < 8
if (g === 4) {
four.A.push(tourney);
} else if (g === 3) {
four.B.push(tourney);
} else if (g === 2) {
four.C.push(tourney);
} else if (g === 1) {
four.D.push(tourney);
}
arr.splice(0, q)
g--;
p++;
z = 0;
x = 0;
y = 0;
}
return four
}
console.log(createMinor4(players4, minorTourny))
当您在 Array 上使用 splice
时,它也会更改原始 Array 这就是为什么 players4
和 minorTourny
在 createMinor4
函数调用后不相同的原因
您可以使用 Deep copy
当您使用深拷贝时,它将不再引用原始数组
What is the most efficient way to deep clone an object in JavaScript?
let players4 = [
"Lava Lamps", "Jarheads", "Cloud Gazerz", "Cobras", "Red Ravens", "Leaders", "Destroyers", "shooters",
"Yellow Dragons", "Whales", "fearwolves", "Hitters", "red rebels", "Knights", "cute gremlins", "water pistols",
"Snakes", "Ravens", "Chariots-of-Hell", "Predators", "Assassins", "Gladiators", "Broncos", "Hawks",
"Gamers", "Slickers", "Freeze", "Rabbits", "Holy Rollers", "Crusaders", "FireHawks", "Worms"
]
let minorTourny = [
["t1", "t2"],
["t3", "t4"],
["t5", "t6"],
["t7", "t8"],
["a", "b"],
["c", "d"],
["e", "f"],
["g"]
];
function createMinor4(_arr, _tourney) {
const arr = JSON.parse(JSON.stringify(_arr)); // Use Deep copy
const tourney = JSON.parse(JSON.stringify(_tourney)); // Use Deep copy
let q = arr.length * .25;
let s = 0;
let p = 1;
let g = 4;
let x = 0;
let y = 0;
let z = 0;
let four = {
"A": [],
"B": [],
"C": [],
"D": []
}
//while 4 > 0
while (g > s) {
do {
let loadTeam = tourney[x].splice(y, 1, arr[z]);
if (z === 0) {
loadTeam;
y++;
} else if (z % 2 == 0) {
loadTeam;
y = 1;
} else if (z % 2 !== 0) {
loadTeam;
y = 0;
x++;
}
z++;
}
while (z < q) // while 0 < 8
if (g === 4) {
four.A.push(tourney);
} else if (g === 3) {
four.B.push(tourney);
} else if (g === 2) {
four.C.push(tourney);
} else if (g === 1) {
four.D.push(tourney);
}
arr.splice(0, q)
g--;
p++;
z = 0;
x = 0;
y = 0;
}
return four
}
console.log(createMinor4(players4, minorTourny))