Javascript中非常奇怪的行为。代码运行不正常

Javascript中非常奇怪的行为。代码运行不正常,javascript,Javascript,我很难描述这一点,因为我无法重新创建任何孤立的实例。在if语句中设置布尔值在它到达if语句之前就设置好了。我原以为这与控制台使日志出现无序有关,但现在我可以在发生时的j for循环中看到结果,除非我删除notesObj.trans.all.combined[I].played=true if(shapesNotes.length === 2){ if(shapesNotes[1].trans.all.combined.length == 2){ console.log(&

我很难描述这一点,因为我无法重新创建任何孤立的实例。在if语句中设置布尔值在它到达if语句之前就设置好了。我原以为这与控制台使日志出现无序有关,但现在我可以在发生时的j for循环中看到结果,除非我删除
notesObj.trans.all.combined[I].played=true

if(shapesNotes.length === 2){
    if(shapesNotes[1].trans.all.combined.length == 2){
        console.log("troubled value = " + shapesNotes[1].trans.all.combined[1].played)
    }

}

function setTranspositionsForAllNotes(notesObj,debugIndex){

    for(var i=0; i<notesObj.trans.all.combined.length; ++i){
        var transNoteAt = notesObj.trans.all.combined[i].noteAt
        var transPlayed = notesObj.trans.all.combined[i].played
        
        console.log("debugIndex = " + debugIndex + "   i = " + i)
        console.log("notesObj.trans.all.combined.length = " +notesObj.trans.all.combined.length)
        console.log("notesObj.trans.all.combined["+i+"].played = " + notesObj.trans.all.combined[i].played)
        console.log("shapesNotes["+debugIndex+"].trans.all.combined["+i+"].played = " + shapesNotes[debugIndex].trans.all.combined[i].played)

        console.log("noteAt = "  + noteAt + " transNoteAt = "  + transNoteAt)

        if(transPlayed === false && 
           noteAt >= transNoteAt){
           
           var transInterval = notesObj.trans.all.combined[i].interval

           for(var j=0; j<notesObj.trans.array.length; ++j){
             notesObj.trans.array[j] += transInterval
             notesObj.trans.freqsPlusTrans[j] = getFrequencyPlusTranspositionForFreqIndex(notesObj,j)
           }
           
           console.log("transInterval = " + transInterval + " for " + debugIndex + "   i = " + i)
          

           notesObj.trans.all.combined[i].played = true
           
        
        }
    
    }
        
}

var debugIndex = 0;
shapesNotes.forEach(function(shapeNotes) {
    setTranspositionsForAllNotes(shapeNotes,debugIndex)
            
    debugIndex+=1
});

请注意,根据日志,对于组合[1]的shapeNotes[1],我的“问题值”设置为false,但当它在我的第一个for循环中达到该值时,它会立即以某种方式设置为true,并且内部的j for循环或控制台日志永远不会出现。奇怪的是,
transPlayed===false&¬eAt>=transNoteAt
条件中的布尔设置
notesObj.trans.all.combined[i].played=true
立即发生,同时该条件中的其他代码均未执行,即使我设置了一个断点。

也许你应该将++I更改为I++

这是一个有趣的建议,但不幸的是,这没有任何效果。数组中的两个条目是否可能引用同一个对象,其中一个您已经将其played属性设置为true?这听起来很可能是命名(
all.combined
)表示它们可能是同一个对象引用。是否返回true<代码>shapesNotes[0].trans.all.combined[1]==shapesNotes[1].trans.all.combined[1]就像测试一样:在每行/函数/代码块的末尾使用分号(如预期)。我很好奇,看看它是否改变了什么。*当然:在…之前保存一个副本。数组中的对象是相同的引用-尽管concat将返回新的数组引用
1980 troubled value = false
1981 debugIndex = 0   i = 0
1982 notesObj.trans.all.combined.length = 2
1983 notesObj.trans.all.combined[0].played = false
1984 shapesNotes[0].trans.all.combined[0].played = false
1986 noteAt = 0.008333333333333333 transNoteAt = 0
1998 transInterval = 0 for 0   i = 0
1981 debugIndex = 0   i = 1
1982 notesObj.trans.all.combined.length = 2
1983 notesObj.trans.all.combined[1].played = false
1984 shapesNotes[0].trans.all.combined[1].played = false
1986 noteAt = 0.008333333333333333 transNoteAt = 0
1998 transInterval = 2 for 0   i = 1
1981 debugIndex = 1   i = 0
1982 notesObj.trans.all.combined.length = 2
1983 notesObj.trans.all.combined[0].played = false
1984 shapesNotes[1].trans.all.combined[0].played = false
1986 noteAt = 0.008333333333333333 transNoteAt = 0
1998 transInterval = 0 for 1   i = 0
1981 debugIndex = 1   i = 1
1982 notesObj.trans.all.combined.length = 2
1983 notesObj.trans.all.combined[1].played = true
1984 shapesNotes[1].trans.all.combined[1].played = true
1986 noteAt = 0.008333333333333333 transNoteAt = 0