2011-06-16 53 views
2

當執行下面的代碼時,螢火蟲告訴我:values [this.geo.value]是undefined 這是什麼問題?爲什麼數組未定義?

$.get('./RDFexamples/tin00089_test2.rdf', null, function (rdfXml) { 
var rdf, json = {}; 
var values = new Array(); 

rdf = $.rdf() 
    .load(rdfXml) 
    .prefix('', 'http://ontologycentral.com/2009/01/eurostat/ns#') 
    .prefix('qb', 'http://purl.org/linked-data/cube#') 
    .prefix('rdf', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#') 
    .prefix('dcterms', 'http://purl.org/dc/terms/') 
    .prefix('sdmx-measure', 'http://purl.org/linked-data/sdmx/2009/measure#') 
    .where('?observation a qb:Observation') 
    .where('?observation dcterms:date ?date') 
    .where('?observation sdmx-measure:obsValue ?measure') 
    .where('?observation :geo ?geo') 
    .each(function() { 
     values[this.geo.value].push(this.measure.value); 
     //alert(this.date.value) 
     //alert(this.measure.value) 
     //alert(this.geo.value) 
    } 
    ); 
    alert(values); 
}); 

回答

3

值[this.geo.value]從來都不是初始化,所以你不能做.push,因爲values [this.geo.value]是未定義的,你首先需要在values [this.geo.value]中創建一個數組,然後才能將它推入。

的僞代碼示例

if values[this.geo.value] == undefined { 
    values[this.geo.value] = [] 
} 
values[this.geo.value].push(...) 
2

push是Array對象本身的方法 - 要調用它在陣列內的值(這可能尚未設定,因此「未定義」)。目前還不清楚是什麼this.geo.value是的,但假設你正試圖設置數組項目的索引,你的選擇是:

values.push(this.measure.value); 

values[this.geo.value] = this.measure.value;