简体   繁体   English

for in 循环仅适用于对象中的最后一个键

[英]for in loop only applying to last key in object

heres the objects to be run through :下面是要运行的对象:

var content = {

book_1: {

    nameyo: "Doctor who",
    subject: "Books",
    price: "$10,000",
    tags: ["BOOK", "FUNNY", "KIDS", "STUPID"]

},

book_2: {

    nameyo: "Chica boom",
    subject: "Books",
    price: "$10,000",
    tags: ["BOOK", "FUNNY", "KIDS", "STUPID"]

},

Album_1: {

    nameyo: "Beatles",
    subject: "Music",
    price: "$10,000",
    tags: ["MUSIC", "BEATLES", "GOOD", "STUPID"]

},

Album_2: {

    nameyo: "ACDC",
    subject: "Music",
    price: "$10,000",
    tags: ["MUSIC", "ACDC", "GOOD", "STUPID"]

}

};

and here's the jquery这是jquery

function createIDForName(key) {
return key + "blah";
}
function createIDForType(key) {
return key + "okay";
}
function createIDForPrice(key){
return key+"stuff";
}

for (var key in content) {
    $(".row").append($("<div class=col-3>")).append($("<p class=name>")).append($("<p class=type>")).append($("<p class=price>"));


    $(".name").attr("id", createIDForName(key));

    $(".type").attr("id", createIDForType(key));

    $(".price").attr("id", createIDForPrice(key));

}

So when this all generates, it only does it for the last key in the content object, Album_2.因此,当所有这些都生成时,它只对内容对象 Album_2 中的最后一个键执行此操作。 Why is this?为什么是这样? Or is it overriding each time?还是每次都覆盖? If yes, why so?如果是,为什么会这样? Any help is appreciated.任何帮助表示赞赏。

You could also use the .each() method to do this.您也可以使用 .each() 方法来执行此操作。 Additionally I condensed your createID() functions into one function with another argument, and made the JSON valid.此外,我将您的createID()函数压缩为一个带有另一个参数的函数,并使 JSON 有效。 DEMO演示

var content = {
    'book_1': {
        'nameyo': "Doctor who",
        'subject': "Books",
        'price': "$10,000",
        'tags': ["BOOK", "FUNNY", "KIDS", "STUPID"]
    },
    'book_2': {
        'nameyo': "Chica boom",
        'subject': "Books",
        'price': "$10,000",
        'tags': ["BOOK", "FUNNY", "KIDS", "STUPID"]
    },
    'Album_1': {
        'nameyo': "Beatles",
        'subject': "Music",
        'price': "$10,000",
        'tags': ["MUSIC", "BEATLES", "GOOD", "STUPID"]
    },
    'Album_2': {
        'nameyo': "ACDC",
        'subject': "Music",
        'price': "$10,000",
        'tags': ["MUSIC", "ACDC", "GOOD", "STUPID"]
    }
};

function createID(key, type) {
    return key + type;
}

$.each(content, function(key, val){
    var col  = '<div class="col-3"><p class="name" id="' + createID(key, 'blah') + '"></p><p class="type" id="' + createID(key, 'okay') + '"></p><p class="price" id="' + createID(key, 'stuff') + '"></p></div>';
    $(".row").append(col);
});

In the result you will have several dom elements with the same classes: .name, .type, .price and then you get them all and override their values over and over again.在结果中,您将有几个具有相同类的 dom 元素: .name, .type, .price ,然后您将获得所有这些.name, .type, .price.name, .type, .price覆盖它们的值。 So in the result you have the last key values;所以在结果中你有最后一个键值;

The selectors $(".name") , $(".price") etc. matches all elements with the class name , not just the one you just appended, but the others you've appended before as well, so you end up with the same values for all of them.选择器$(".name")$(".price")等匹配所有具有类name元素,不仅是你刚刚附加的元素,还有你之前附加的其他元素,所以你最终所有这些都具有相同的值。

To avoid that, and make life easier, just keep a reference to the elements为了避免这种情况,并使生活更轻松,只需保留对元素的引用

for (var key in content) {
   var _name = $('<p />', {'class' : 'name'}); //note that window.name exists
                                               // be careful with the name "name"

   _name.attr("id", createIDForName(key));

The easiest would be to just add the ID when the elements are created最简单的方法是在创建元素时添加 ID

for (var key in content) {
    var _col  = $('<div />', {'class' : 'col-3'}),
        _name = $('<p />', {'class' : 'name',  id : createIDForName(key)}),
        _type = $('<p />', {'class' : 'type',  id : createIDForType(key)}),
        _pric = $('<p />', {'class' : 'price', id : createIDForPrice(key)});

    $(".row").append(_col, _name, _type, _pric);
}

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

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