2

我已经仔细研究了为什么会发生这种情况。我有一个脚本,它使用 AJAX 获取本地 JSON 文件并将它们合并在一起,以便以后可以在 HTML 表中显示数据。

该脚本可以很好地在 Chrome 控制台中显示对象。但我无法访问特定元素,例如“object.country”。关于如何解决这个问题的任何帮助?

控制台输出(缩短):

(3) [Array(244), "success", {…}]
0: Array(244)
[0 … 99]
0: {country: "Afghanistan", city: "Kabul", continent: "Asia", costline: 0, currency_name: "Afghanistan Afghani", …}

脚本

function onLoad() {
    $.when(
        $.ajax({
            url: "./country-objects/country-by-capital-city.json",
            type: "GET",
            dataType: 'JSON'
        }),
        $.ajax({
            url: "./country-objects/country-by-continent.json",
            type: "GET",
            dataType: 'JSON'
        }),
        $.ajax({
            url: "./country-objects/country-by-costline.json",
            type: "GET",
            dataType: 'JSON'
        }),
        $.ajax({
            url: "./country-objects/country-by-currency-name.json",
            type: "GET",
            dataType: 'JSON'
        }),
        $.ajax({
            url: "./country-objects/country-by-flag.json",
            type: "GET",
            dataType: 'JSON'
        }),
        $.ajax({
            url: "./country-objects/country-by-domain-tld.json",
            type: "GET",
            dataType: 'JSON'
        })).then(function (res1, res2, res3, res4, res5, res6) {

        const object = $.extend(true, res1, res2, res3, res4, res5, res6);

        console.log(object);
    })
}

- 编辑 -

每个 JSON 文件的示例对象(每个文件的第一个条目)。以相同顺序 az 的所有文件:

// /country-objects/country-by-capital-city.json
  {
    "country": "Afghanistan",
    "city": "Kabul"
  }

// /country-objects/country-by-continent.json
{
    "country": "Afghanistan",
    "continent": "Asia"
  }

// /country-objects/country-by-costline.json
{
    "country": "Afghanistan",
    "costline": 0
  }

// /country-objects/country-by-currency-name.json

{
    "country": "Afghanistan",
    "currency_name": "Afghanistan Afghani"
  }

// /country-objects/country-by-flag.json 
{
    "country": "Afghanistan",
    "flag_base64": "data:image\/svg+xml;base64,PHN2ZyB4bWxucz0ia......"
}

// /country-objects/country-by-domain-tld.json
{
    "country": "Afghanistan",
    "tld": ".af"
  }

// Goal, continue for each country

{
"country": "Afghanistan",
        "city": "Kabul",
        "continent": "Asia",
        "costline": 0,
        "currency_name": "Afghanistan Afghani",
        "flag_base64": "data:image\/svg+xml;base64,PHN2ZyB4bWxucz0ia......",
        "tld": ".af"
},
4

2 回答 2

0

我可以像这样遍历数组。

for (let i = 0;  i < object.length; i++) {

                let obj = object[i];
                obj.forEach(function (item) {
                    console.log(item.country+" | "+item.continent);
                });
            }

感谢@Frenchy 指出这一点。

于 2021-03-02T14:26:43.860 回答
0

您可以迭代或过滤:

var data = [{
    "country": "Afghanistan",
            "city": "Kabul",
            "continent": "Asia",
            "costline": 0,
            "currency_name": "Afghanistan Afghani",
            "flag_base64": "data:image\/svg+xml;base64,PHN2ZyB4bWxucz0ia......",
            "tld": ".af"
    },
    {
    "country": "France",
            "city": "Paris",
            "continent": "Asia",
            "costline": 0,
            "currency_name": "Euro",
            "flag_base64": "data:image\/svg+xml;base64,PHN2ZyB4bWxucz0ia......",
            "tld": ".af"
    },  ];
  //iterate
    data.forEach(function(rec, index){

            console.log(rec.country, rec.city);
    });
  
  var datafilter = data.filter( r => r.country == "France");
  console.log(datafilter)
  
  datafilter = data.filter( r => r.costline == 0);
    console.log(datafilter)

于 2021-03-02T14:31:02.850 回答