如何在Javascript

时间:2015-10-27 08:02:29

标签: javascript multidimensional-array

您好我已经将谷歌电子表格中的数据捕获到像这样的新数组

$.getJSON("https://spreadsheets.google.com/feeds/list/1IoNqeReOPKNFrYMlK2rnJVuMaLeAgnZneLbKYSQ7bs4/od6/public/values?alt=json", function(data) {
      //$.getJSON("http://cors.io/spreadsheets.google.com/feeds/list/1IoNqeReOPKNFrYMlK2rnJVuMaLeAgnZneLbKYSQ7bs4/od6/public/values?alt=json", function(data) {
    var len = data.feed.entry.length;
    for (var i=0; i<len; i++) {
      //first row "title" column
      resultData[i]=new Array(9);
      resultData[i][1]=data.feed.entry[i].gsx$constituency.$t;
      resultData[i][2]=data.feed.entry[i].gsx$winner2010.$t;
      resultData[i][3]=data.feed.entry[i].gsx$winningparty.$t;
      resultData[i][4]=data.feed.entry[i].gsx$candidatename1.$t;
      resultData[i][5]=data.feed.entry[i].gsx$party1.$t;
      resultData[i][6]=data.feed.entry[i].gsx$candidatename2.$t;
      resultData[i][7]=data.feed.entry[i].gsx$party2.$t;
      resultData[i][8]=data.feed.entry[i].gsx$candidatename3.$t;
      resultData[i][9]=data.feed.entry[i].gsx$party3.$t;
    }

  });

如何将上述内容重写为下面的文字数组

var colors = [gsx $ constituency,gsx $ winner2010];

我仍在学习编写代码,所以会感激任何帮助

3 个答案:

答案 0 :(得分:1)

我认为你应该更新你的代码,如下所示:

$.getJSON("https://spreadsheets.google.com/feeds/list/1IoNqeReOPKNFrYMlK2rnJVuMaLeAgnZneLbKYSQ7bs4/od6/public/values?alt=json", function(data) {

    data.feed.entry.forEach(function(singleEntry) {
        var resultData = [];
        resultData.push(singleEntry);
    })

});

现在您可以获取 resultData [index] .gsx $ constituency 等数据。

答案 1 :(得分:1)

这是一种将所有值放在二维数组中的方法:

var resultData = [];
$.getJSON("https://spreadsheets.google.com/feeds/list/1IoNqeReOPKNFrYMlK2rnJVuMaLeAgnZneLbKYSQ7bs4/od6/public/values?alt=json", function(data) {   
  var len = data.feed.entry.length, entries = data.feed.entry;
  for (var i=0; i<len; i++) {
     var row = [];
     for (var name in entries[i]) row.push(entries[i][name].$t);
     resultData.push(row);
  }
  console.log('result:', resultData);
});

小提琴:http://jsfiddle.net/5zycjq7c/

答案 2 :(得分:0)

$.getJSON("https://spreadsheets.google.com/feeds/list/1IoNqeReOPKNFrYMlK2rnJVuMaLeAgnZneLbKYSQ7bs4/od6/public/values?alt=json", function(data) {
  var len = data.feed.entry.length;

  for (var i=0; i<len; i++) {
    //first row "title" column
    var current = data.feed.entry[i];
    resultData[i]= [current.gsx$constituency.$t, current.gsx$winner2010.$t, current.gsx$winningparty.$t, ...]; // include all here
  }
});