Javascript 为什么chrome inspector在扩展时为数组显示一件事而为另一件事
我很难理解为什么在我展开数组或对象时,检查器会更改其日志记录:Javascript 为什么chrome inspector在扩展时为数组显示一件事而为另一件事,javascript,arrays,Javascript,Arrays,我很难理解为什么在我展开数组或对象时,检查器会更改其日志记录: let newTeamArray = [...teams]; // teams = {randomTeamObject, "Fill"} let teamList1 = []; let teamList2 = []; for(let x = 0; x < newTeamArray.length; x++) { if(x < (newTeamArray.length/2)) {
let newTeamArray = [...teams]; // teams = {randomTeamObject, "Fill"}
let teamList1 = [];
let teamList2 = [];
for(let x = 0; x < newTeamArray.length; x++) {
if(x < (newTeamArray.length/2)) {
console.log("Push to 1")
console.log(newTeamArray[x])
teamList1.push(newTeamArray[x]);
} else {
console.log("Push to 2")
console.log(newTeamArray[x])
teamList2.push(newTeamArray[x])
}
}
console.log(teamList1)
console.log(teamList2)
让newTeamArray=[…团队];//团队={randomTeamObject,“填充”}
让teamList1=[];
让teamList2=[];
for(设x=0;x
这给了我:
有人能解释为什么最后两个console.log()命令的扩展版本在扩展时不同吗。如果我直接调用teamList1[0],它就没有定义,如检查器所示
编辑:称为“teams”的数组始终是偶数长度的数组。如果数组长度为奇数,则将“Fill”添加到数组中。由于此数组只有一个对象,因此添加了fill。这发生在上面的代码之前。我的目标是将偶数数组拆分为两个大小相同的数组,称为teamList1和teamList2类似的问题和问题都有答案。
基本上,当您展开对象时,打印到控制台的对象是“惰性”计算的,因此,如果代码中其他地方对同一对象的引用对该对象进行了更改,则在展开对象时在控制台中看到的值将反映该更改。这是假设您正在使用屏幕截图所示的Chrome调试器。正如Kel所回答的,Chrome inspector在异步或惰性环境中活动。下面的代码在两个数组之间执行push/pop操作。我正在将第二个数组的唯一对象删除为静态变量。然后,当我在两个数组之间旋转元素时,teamList2为空,无法将其任何元素传输到第一个数组。以下是导致问题的代码:
function makeSeasonForComp(teams) {
let season = [];
let newTeamArray = [...teams];
let teamList1 = [];
let teamList2 = [];
let statcTeam;
console.log(teams)
for(let x = 0; x < newTeamArray.length; x++) {
if(x < (newTeamArray.length/2)) {
console.log("Push to 1")
console.log(newTeamArray[x])
teamList1.push(newTeamArray[x]);
} else {
console.log("Push to 2")
console.log(newTeamArray[x])
teamList2.push(newTeamArray[x])
}
}
statcTeam = teamList2.pop();
for(let i = 0; i < SEASONLENGTH; i++) {
let games = [];
if(teamList1.length > 0) {
for(let j = 0; j < teamList1.length; j++) {
let game = [];
if (j === 0) {
if(teamList1[0] === "fill") {
game.push(teamList1[0])
} else {
game.push(teamList1[0].uid)
}
game.push(statcTeam);
game.push(uuidv4())
} else {
if(teamList1[j] === "fill") {
game.push(teamList1[j])
} else {
game.push(teamList1[j].uid)
}
if(teamList2[j - 1] === "fill") {
game.push(teamList2[j - 1])
} else {
game.push(teamList2[j - 1].uid)
}
game.push(uuidv4())
}
games.push(game);
}
if(teamList2.length > 0) { // This line fixed my issue
let endList1 = teamList1.pop();
let startList2 = teamList2.shift();
teamList2.push(endList1);
teamList1.unshift(startList2);
}
}
season.push(games);
}
函数makeSeasonForComp(团队){
让季节=[];
让newTeamArray=[…团队];
让teamList1=[];
让teamList2=[];
让STATCHEAM;
控制台日志(团队)
for(设x=0;x0){
for(设j=0;j0){//此行解决了我的问题
让endList1=teamList1.pop();
设startIST2=teamList2.shift();
teamList2.推送(endList1);
团队列表1.取消移动(2);
}
}
季节。推(游戏);
}
我弹出了teamList2中唯一的元素,使得正在进行的pop操作返回未定义的数组
在旋转阵列之前检查我的teamList2中的元素修复了我的问题
谢谢!这是Chrome上多年来的一个问题-我不知道他们怎么还没有修复它。如果你通过控制台进行大量类似的调试,我会尝试使用Firefox。