2017-12-06 4 views
0

配列を並べ替えるとマップは使えません。配列をAxiosから並べ替える

var arrQuestByDate = new Array(); 
action.payload.data.forEach(function(item){ 
    if(typeof(arrQuestByDate[item.date])==='undefined'){ 
    arrQuestByDate[item.date]= new Array(); 
    } 
    arrQuestByDate[item.date].push(item); 
    console.log(arrQuestByDate[item.date].length) //this output good value 
}) 
console.log(arrQuestByDate.length) //this output 0 

私は、配列の長さは、あなたがlodashからgroupBy機能を使用することができます0

+0

大括弧の構文はオブジェクトであり、配列ではありません。 –

+0

コード全体を投稿する必要があります... – assembler

+0

「arrQuestByDate [item.date]」とは何ですか? –

答えて

1

である理由を知りません。 外部ライブラリを使用したくない場合は、このコードを使用してください。

var quotestByDate = action.payload.data 
    .reduce(function(acc, quote) {  
     if (!acc[quote.date]) { 
      acc[quote.date] = []; 
     } 
     acc[quote.date].push(quote); 
     return acc; 
    }, {}); 

console.log(Object.keys(quotestByDate).length);