0

If I sort array I cant use map.

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 

I dont know why my array length is 0

6
  • The square bracket syntax is for objects, not arrays. Commented Dec 6, 2017 at 14:07
  • you should post your entire code... Commented Dec 6, 2017 at 14:07
  • What is that arrQuestByDate[item.date] ? Commented Dec 6, 2017 at 14:12
  • Looking at your code, arrQuestByDate should be object. Please share input and expected output. Commented Dec 6, 2017 at 14:13
  • 1
    There is nothing related to Axios nor to React in this code, your issue may lie in code you did not shared Commented Dec 6, 2017 at 14:35

1 Answer 1

1

You can use groupBy function from lodash. If you don't want to use external library, this code shoudl work

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); 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.