Javascript 对象在指定任何对象之前具有值

Javascript 对象在指定任何对象之前具有值,javascript,json,object,Javascript,Json,Object,我不知道如何使这个问题更精确 我有这张桌子。如您所见,每行中都有一些复选框,并且选中的值被分配到具有如下所示结构的多维对象中 选中的值是从JSON文件加载的,当您按save时,它会创建一个新对象,该对象假定具有相同的结构,但具有更新的值,然后覆盖JSON文件 守则: function saveRecruit() { var obj = {}; var specsObj = {}; var classesObj = {}; var

我不知道如何使这个问题更精确

我有这张桌子。如您所见,每行中都有一些复选框,并且
选中的
值被分配到具有如下所示结构的多维对象中

选中的
值是从JSON文件加载的,当您按save时,它会创建一个新对象,该对象假定具有相同的结构,但具有更新的值,然后覆盖JSON文件

守则:

function saveRecruit() {
        var obj = {};
        var specsObj = {};
        var classesObj = {};
        var rowCount = $("#classTable tr").length;
        var columnCount;
        var jsonChar;
        var jsonSpec;
        var jsonSpecRC;
        var jsonString;
        console.log("------ LOGGING FIRST: specsObj ------");
        console.log(specsObj);
        for(x = 1;x<=rowCount;x++)
        {
            jsonChar = $("#classTable tr:nth-child(" + x + ") td:nth-child(1)").text();
            columnCount = $("#classTable tr:nth-child(" + x + ")").children().length;

            for(y = 2;y<=columnCount;y++)
            {
                jsonSpec = $("#classTable tr:nth-child(" + x + ") td:nth-child(" + y + ")").text();
                jsonSpecRC = $("#classTable tr:nth-child(" + x + ") td:nth-child(" + y + ") input:nth-child(2)")[0].checked;
                console.log("Setting " + jsonSpec + " to " + jsonSpecRC + " in specsObj");
                console.log("X: " + x + " - Y: " + y);
                specsObj[jsonSpec] = jsonSpecRC;
            };
            obj[jsonChar] = specsObj;
            console.log(jsonChar + ": specsObj");
            console.log(specsObj);
        };
        console.log("Logging obj");
        console.log(obj);
        console.log(jsonString);

        $("#jsonInput").val(jsonString);
        return false;
    };
变量
specsObj
在分配任何值之前被声明和记录。您可以在下图中看到日志:


问题在于,在循环中,您不断添加到
specsObj
,而没有为每一行重置

这是您的代码,添加了一行代码来解决问题

for(x = 1;x<=rowCount;x++)
    {
        jsonChar = $("#classTable tr:nth-child(" + x + ") td:nth-child(1)").text();
        columnCount = $("#classTable tr:nth-child(" + x + ")").children().length;

        specsObj = {}; // add this to reset the specsObj for each character

        for(y = 2;y<=columnCount;y++)
        {
            jsonSpec = $("#classTable tr:nth-child(" + x + ") td:nth-child(" + y + ")").text();
            jsonSpecRC = $("#classTable tr:nth-child(" + x + ") td:nth-child(" + y + ") input:nth-child(2)")[0].checked;
            console.log("Setting " + jsonSpec + " to " + jsonSpecRC + " in specsObj");
            console.log("X: " + x + " - Y: " + y);
            specsObj[jsonSpec] = jsonSpecRC;
        };
        obj[jsonChar] = specsObj;
        console.log(jsonChar + ": specsObj");
        console.log(specsObj);
    };

for(x=1;xOf当然,不知道我是怎么想得太多的。但是,你能告诉我为什么它在分配任何东西之前记录了填充对象吗?编辑:另外,非常感谢你的快速回答:)谢谢it@StevoHN这是因为在chrome中,它会记录活动对象,所以当您转到控制台并打开它时,它已经用循环中发生的任何事情进行了更新。好的,非常感谢您的澄清。所以我不能真的依靠它来排除故障。我已经接受了你的回答。如果你正在记录整个对象,就不会接受。如果您尝试记录一个特定的属性,您将得到预期的
未定义的
。或者,您可以为日志
console.log(JSON.parse(JSON.stringify(specsObj))创建对象的深度副本我明白了。然而,我刚刚意识到,脚本显然正在被缓存,而JSON对象也在缓存中,这意味着我需要在显示信息的首页上强制重新加载脚本,或者将复选框更改为巨大的形式并将其存储在数据库中。你能推荐一个吗?
for(x = 1;x<=rowCount;x++)
    {
        jsonChar = $("#classTable tr:nth-child(" + x + ") td:nth-child(1)").text();
        columnCount = $("#classTable tr:nth-child(" + x + ")").children().length;

        specsObj = {}; // add this to reset the specsObj for each character

        for(y = 2;y<=columnCount;y++)
        {
            jsonSpec = $("#classTable tr:nth-child(" + x + ") td:nth-child(" + y + ")").text();
            jsonSpecRC = $("#classTable tr:nth-child(" + x + ") td:nth-child(" + y + ") input:nth-child(2)")[0].checked;
            console.log("Setting " + jsonSpec + " to " + jsonSpecRC + " in specsObj");
            console.log("X: " + x + " - Y: " + y);
            specsObj[jsonSpec] = jsonSpecRC;
        };
        obj[jsonChar] = specsObj;
        console.log(jsonChar + ": specsObj");
        console.log(specsObj);
    };