jQuery使用foreach迭代JSON

时间:2015-03-30 11:58:24

标签: javascript jquery

我有以下代码

currencies_group_0 = [
    {"code":"cad", "big_code":"CAD", "name":"Canadian Dollars", "symbol":"$", "rate" : 0.97},
    {"code":"eur", "big_code":"EUR", "name":"Euros", "symbol":"€", "rate" : 0.7102},
    {"code":"gbp", "big_code":"GBP", "name":"British Pounds Sterling", "symbol":"£", "rate" : 0.5196},
    {"code":"hkd", "big_code":"HKD", "name":"Hong Kong Dollars", "symbol":"$", "rate" : 5.9853},
    {"code":"jpy", "big_code":"JPY", "name":"Japanese Yen", "symbol":"¥", "rate" : 92},
    {"code":"nzd", "big_code":"NZD", "name":"New Zealand Dollars", "symbol":"$", "rate" : 1.0237},
    {"code":"usd", "big_code":"USD", "name":"United States Dollars", "symbol":"$", "rate" : 0.7719}
];

currencies_group_0.forEach(function(element) {
    lis_0 += '' +
    '<li class="ui-selectmenu-item-label" rate="' + element.rate + '">' +
    '<a href="#nogo" tabindex="-1">' +
    '<span>' +
    '<div class="flag ' + element.code + '"></div>' +
    '<span class="ui-selectmenu-item-content">' +
    '<span class="currency-name">' + element.name + '</span>' +
    '<span class="currency-code">' + element.symbol + ' ' + element.big_code + '</span>' +
    '</span>' +
    '</span>' +
    '</a>' +
    '</li>';
});

“元素”有什么作用?我不确定这段代码是如何工作的。不应该在某个地方有“这个”吗?

4 个答案:

答案 0 :(得分:1)

在循环数组时,element是该数组中的每个对象。

+=正在整合数组元素并创建html。

var lis_0 = '';

currencies_group_0 = [
    {"code":"cad", "big_code":"CAD", "name":"Canadian Dollars", "symbol":"$", "rate" : 0.97},
    {"code":"eur", "big_code":"EUR", "name":"Euros", "symbol":"€", "rate" : 0.7102},
    {"code":"gbp", "big_code":"GBP", "name":"British Pounds Sterling", "symbol":"£", "rate" : 0.5196},
    {"code":"hkd", "big_code":"HKD", "name":"Hong Kong Dollars", "symbol":"$", "rate" : 5.9853},
    {"code":"jpy", "big_code":"JPY", "name":"Japanese Yen", "symbol":"¥", "rate" : 92},
    {"code":"nzd", "big_code":"NZD", "name":"New Zealand Dollars", "symbol":"$", "rate" : 1.0237},
    {"code":"usd", "big_code":"USD", "name":"United States Dollars", "symbol":"$", "rate" : 0.7719}
];

currencies_group_0.forEach(function(element) {

        lis_0 += '' +
        '<li class="ui-selectmenu-item-label" rate="' + element.rate + '">' +
        '<a href="#nogo" tabindex="-1">' +
        '<span>' +
        '<div class="flag ' + element.code + '"></div>' +
        '<span class="ui-selectmenu-item-content">' +
        '<span class="currency-name">' + element.name + '</span>' +
        '<span class="currency-code">' + element.symbol + ' ' + element.big_code + '</span>' +
        '</span>' +
        '</span>' +
        '</a>' +
        '</li>';


});

document.getElementById('html').innerHTML = lis_0;

https://jsfiddle.net/w2rypfoo/1/

答案 1 :(得分:0)

“element”充当循环中当前项的局部变量引用,而“this”将引用选择器的引用。

答案 2 :(得分:0)

实际上你正在使用forEach,因此它将逐个迭代每个元素的Array。因此,currency_group_0 [0]将首次作为元素。

答案 3 :(得分:0)

我认为您可以从Array.prototype.forEach文档中轻松了解这一点。 这里每个元素都有每行或多个项目。 例如

0 =&gt; {“code”:“cad”,“big_code”:“CAD”,“name”:“Canadian Dollars”,“symbol”:“$”,“rate”:0.97}

1 =&gt; {“code”:“eur”,“big_code”:“EUR”,“name”:“Euros”,“symbol”:“€”,“rate”:0.7102}

......等等非常直接。名称“元素”

也来自docs

  

使用三个参数调用回调:

     

元素值
  元素指数
  正在遍历的数组