使用来自硬币市值 API 的数组获取特定的 JSON 数据值



我想使用数组中的"name"获取符号和其他信息。

$.getJSON("//api.coinmarketcap.com/v1/ticker/?limit=0", function(data) {
    var crypto = [
      "Ethereum",
      "Ripple",
      "Tron", 
    ];
    // used for array to get length
    var arrayLength = crypto.length;
    for (var i = 0; i < data.length; i++) {
        console.log( data[i].name == crypto[i] );
    }
  });

我尝试使用:

console.log( data[i].name == crypto[i] );

但无法让这个工作。到目前为止,当我只想列出数组中项目的数据时,它会列出所有内容。

我想使用名称获取数组中的数据,以从 API 检索其信息。

任何帮助,不胜感激。

使用 inArray 函数。

例:

$.getJSON("//api.coinmarketcap.com/v1/ticker/?limit=0", function(data) {
    var crypto = [
      "Ethereum",
      "Ripple",
      "Tron", 
    ];
    // used for array to get length
    var arrayLength = crypto.length;
    for (var i = 0; i < data.length; i++) {
        if(jQuery.inArray(data[i].name, crypto) !== -1)
            console.log('Finded: ' + data[i].name);
    }
  });

最新更新