简体   繁体   English

Javascript for ... in循环中的objetcs没有在最后一个属性上运行

[英]Javascript for…in loop for objetcs not running on last property

this is my first post in stackoverflow.. I am trying to iterate over an object(my implementation is an associative array) which in turn has some properties. 这是我在stackoverflow中的第一篇文章。我试图迭代一个对象(我的实现是一个关联数组),而这又有一些属性。 Now I wish to construct another array out of it in order to use it as a localsource in jquery autocomplete widget for seach operations. 现在我想构建另一个数组,以便在jquery自动完成小部件中将其用作seals操作的localsource。 Now the problem is that i am using for in loop to that according to the documenations available... However the output is always one less than the original object. 现在问题是我根据可用的文档使用for循环...然而输出总是比原始对象少一个。 The itearation involving the last element is not performed at all. 根本不执行涉及最后一个元素的迭代。 Below is the sample object that I am using as input. 下面是我用作输入的示例对象。

    SubTeachPair = object{"5CS1":{SubAbbrev:"CA-L",SubCode:"5CS1",SubName:"Computer Architecture",TeacherId:"1",TeacherName:"Ayush Pandey",label:"Computer Architecture",value:"5CS1"},"5CS2":{SubAbbrev:"CA-P",SubCode:"5CS2",SubName:"Computer Engg",TeacherId:"10",TeacherName:"MAyush Pandey",label:"Computer Engg",value:"5CS2"}}

It has this kind of elements and is dynamically generated so the property names are variable. 它具有这种元素并且是动态生成的,因此属性名称是可变的。 The loop construct that I have written is 我写的循环结构是

    var SubSource = [];
                console.log(SubTeachPair);
                var count = 0;


                for(sub in SubTeachPair){
                    console.log(count);
                    SubSource[count] = {};
                    SubSource[count]['label']=SubTeachPair[sub]['label'];
                    SubSource[count]['value']=SubTeachPair[sub]['value'];    
                    count++;    
                }

However, the result for the given input is only: 但是,给定输入的结果仅为:

object{{ label: "Computer Architecture", value: "5CS1"}}

Am I missing something here? 我在这里错过了什么吗?

edit-- The function that produces the input object is as follows(It is triggered onclick by the next button). edit--生成输入对象的函数如下(通过下一个按钮触发它)。

    $('#' + $(this).attr("id")).autocomplete({
                                           source : 'search',
                                           minLength : 1,
                                           change : function(event, ui) {
                                           if( typeof ui.item != 'undefined') {
                                           SubTeachPair[$(this).attr("id")] = {};
                                           //   console.log(ui.item);
                                           SubTeachPair[$(this).attr("id")]['value'] =  $(this).attr("id");
                                         SubTeachPair[$(this).attr("id")]['label'] = $('label[for="' + this.id + '"]').html();
                                           SubTeachPair[$(this).attr("id")]['SubCode'] = $(this).attr("id");
                                           SubTeachPair[$(this).attr("id")]['SubName'] =$('label[for="' + this.id + '"]').html();
                                           SubTeachPair[$(this).attr("id")]['SubAbbrev'] =$('label[for="' + this.id + '"]').attr('id');
                                           SubTeachPair[$(this).attr("id")]['TeacherId'] = ui.item.id;
                                            SubTeachPair[$(this).attr("id")]['TeacherName'] = ui.item.value;
                                        //  console.log(SubTeachPair);
                                            //window.SubTeachPair = SubTeachPair;

                                       }
                               }
                           });

I think I have found the cause of the error -- the object that is the input is actually the out put of another form that uses jquery autocomplete . 我想我找到了错误的原因 - 作为输入的对象实际上是另一个使用jquery自动完成的表单的输出。 Now when I enter something in the input and then click on the suggestion, the suggestion is filled in the text input, however if i do not click outside the input text and directly click the button which triggers my script, I get that error. 现在,当我在输入中输入内容然后单击建议时,建议填写在文本输入中,但是如果我没有在输入文本外单击并直接单击触发我的脚本的按钮,则会出现该错误。 Otherwise its fine. 否则罚款。 Is there any way to avoid that? 有什么办法可以避免吗?

In your code, the array SubSource and count are not defined, You have to declare: 在您的代码中, SubSource数组SubSourcecount ,您必须声明:

var SubSource = [];
var count = 0`  

before for(sub in SubTeachPair) {...} 之前for(sub in SubTeachPair) {...}

See http://jsfiddle.net/abu5C/ http://jsfiddle.net/abu5C/

Try this: 尝试这个:

SubSource[count] = {};

for(sub in SubTeachPair) {
    console.log(count);

    SubSource[count]['label']=SubTeachPair[sub]['label'];
    SubSource[count]['value']=SubTeachPair[sub]['value'];

    count++; 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM