0
試圖以動態的方式來引用的陣列時,我有一個問題的元件。我不會總是知道我的用戶會選擇哪個字段,所以我想在數組中進行動態選擇。我希望我可以將字段存儲在一個變量中,然後使用此變量來指定我想從數組中獲得哪個字段。D3JS動態訪問數組
我做了一個簡單的例子來說明我的問題。
如果我訪問數組是這樣的:
console.log('static: ' + nested_data[0].values[0].cat);
在返回「一」,這是我所期待的,但如果我這樣做
然後:
console.log('dynamic :' + nested_data[0].values[0].label);
,其中標籤是一個變量,然後我得到'未定義'。
任何人都知道我做錯了。
// Some Sample data
var data = [
{ value: 01, cat:'a'},
{ value: 40, cat:'a'},
{ value: 80, cat:'b'},
{ value: 120, cat:'b'}
];
//Nest the data on 'cat'
var nested_data = d3.nest()
.key(function(d) { return d.cat; })
.entries(data);
//Print the value of the data[0].values[0].cat
console.log('static: ' + nested_data[0].values[0].cat);
//Dynamic, create a variable to hold the parameter I want to refrence
var label = 'cat';
//Pass the variable to the array
console.log('dynamic :' + nested_data[0].values[0].label);
太感謝你了,它現在這麼明顯!再次感謝。 –