2012-03-30 5 views
0

私はこのようなJSONレスポンス取得しています:どのように私が作ると、このようなデータのを保存することができ、それぞれを使用して配列とストアをどのように組み合わせたカテゴリをマップするのですか?

[ 
{"IndexID":1,"IndexName":"Consumer Confidence Index(CCI)","IndexValue":34.79,"Month":10,"PercentageChanged":0,"Position":0,"Year":2011}, 
{"IndexID":1,"IndexName":"Consumer Confidence Index(CCI)","IndexValue":34.25,"Month":11,"PercentageChanged":0,"Position":0,"Year":2011}, 
{"IndexID":1,"IndexName":"Consumer Confidence Index(CCI)","IndexValue":33.58,"Month":12,"PercentageChanged":0,"Position":0,"Year":2011}, 
{"IndexID":1,"IndexName":"Consumer Confidence Index(CCI)","IndexValue":32.64,"Month":1,"PercentageChanged":0,"Position":0,"Year":2012}, 
{"IndexID":1,"IndexName":"Consumer Confidence Index(CCI)","IndexValue":36.92,"Month":2,"PercentageChanged":0,"Position":0,"Year":2012}, 

{"IndexID":2,"IndexName":"Current Sentiment Index","IndexValue":47.42,"Month":10,"PercentageChanged":0,"Position":0,"Year":2011}, 
{"IndexID":2,"IndexName":"Current Sentiment Index","IndexValue":47.55,"Month":11,"PercentageChanged":0,"Position":0,"Year":2011}, 
{"IndexID":2,"IndexName":"Current Sentiment Index","IndexValue":47.01,"Month":12,"PercentageChanged":0,"Position":0,"Year":2011}, 
{"IndexID":2,"IndexName":"Current Sentiment Index","IndexValue":45.01,"Month":1,"PercentageChanged":0,"Position":0,"Year":2012}, 
{"IndexID":2,"IndexName":"Current Sentiment Index","IndexValue":47.32,"Month":2,"PercentageChanged":0,"Position":0,"Year":2012}, 

{"IndexID":3,"IndexName":"Future Expectations Sentiment Index","IndexValue":29.63,"Month":10,"PercentageChanged":0,"Position":0,"Year":2011}, 
{"IndexID":3,"IndexName":"Future Expectations Sentiment Index","IndexValue":28.81,"Month":11,"PercentageChanged":0,"Position":0,"Year":2011}, 
{"IndexID":3,"IndexName":"Future Expectations Sentiment Index","IndexValue":28.09,"Month":12,"PercentageChanged":0,"Position":0,"Year":2011}, 
{"IndexID":3,"IndexName":"Future Expectations Sentiment Index","IndexValue":27.59,"Month":1,"PercentageChanged":0,"Position":0,"Year":2012}, 
{"IndexID":3,"IndexName":"Future Expectations Sentiment Index","IndexValue":32.67,"Month":2,"PercentageChanged":0,"Position":0,"Year":2012} 

] 

を:

var allData = [[for index id1],[for index id1],[for index id1]],[[for index id2],for index id2,],[[for indexid3],[for indexid3],[for indexid3]]]; 

重要:私はどのように5月のインデックス値を知りません私はそれがすべての徹底的なダイナミックを作成する必要が取得しているデータにあります。

そうですか?それは異なる値を持つカテゴリに応じてグラフを描画するのに役立ちます。これを達成するための簡単な方法は?

+0

'for index id1'はオブジェクトの配列ですか? –

+0

はい、それはオブジェクトの配列だけです。 – 3gwebtrain

答えて

1

私は考えることができるベストは、あなたがIndexIDをキーと単一のオブジェクトにそれを減らすことができ、ループ

// first pass, extract all data and categorize it 
var categories = []; 

for(var i = 0; i < items.length; i++) { 
    if(typeof categories[items[i].IndexID] == 'undefined') categories[items[i].IndexID] = []; 
    categories[items[i].IndexID].push(items[i]); 
} 


// second pass, separate out each indexes to its own array in a big array 
var results = []; 

$(categories).each(function(i, n) { 
    results.push(n); 
}); 
+0

ここからあなたはitmes.lengthを取得していますか? – 3gwebtrain

+0

@ 3gwebtrainこれは標準のプロパティです。javascriptの配列オブジェクトには –

0

のための2つを使用している:

function keyOnId(prev, curr) { 
    if(!(curr.IndexID in prev)) 
     prev[curr.IndexID] = []; 

    prev[curr.IndexID].push(curr); 

    return prev; 
} 

var dataObject = data.reduce(keyOnId, {}); 

あなたは今のようなものを探してオブジェクトdataObjectを持つべきです:

{ 
    '1': [ {IndexID: 1, ...}, {IndexID: 1, ...}, ... ], 
    '2': [ {IndexID: 2, ...}, {IndexID: 2, ...}, ... ], 
    ... 
} 

ここでは、検索の簡単な問題ですこれらの配列をヴィング:

function arrayOfValues(obj) { 
    return Object.keys(obj).map(function(key) { 
    return obj[key]; 
    }); 
} 

var allData = arrayOfValues(dataObject); 

編集:代替ソリューション - IndexID sが数値である場合にのみ動作します。

は、スパース配列にあなたの配列を削減(未定義の値を持つことになり、存在しない、彼らは順番にされていない限り、IndexID秒):

function sparseArray(prev, curr) { 
    if(!prev[curr.IndexID]) 
     prev[curr.IndexID] = []; 

    prev[curr.IndexID].push(curr); 

    return prev; 
} 

var sparse = data.reduce(sparseArray, []); 

さて、filterアウトのみtruthy値:

var allData = sparse.filter(function(val) { return val }); 

更新:

Fiddle of first approach

Fiddle of second approach

警告:Object.keysreducemapfilterはECMAScriptの5の一部であることに注意してください、といないIEで利用可能な、あなたは古いブラウザ用es5 shimを使用する必要がありますので。

+0

があります。両方の方法でprev、currについて詳しく説明できますか?どのように私はすべての配列を関数に渡すことができます – 3gwebtrain

+0

それは 'reduce'がすることです。 [reduce](https://developer.mozilla.org/ja/JavaScript/Reference/Global_Objects/Array/Reduce)を参照してください。 –

+0

あなたはhttp://jsfiddle.net/を使ってさらに説明することができます、私はあなたのオブジェクトベースのコンセプトが大好きですが、私は追いつくことができません!..ごめんなさい! – 3gwebtrain

関連する問題