2

I tried a lot of algorithm to convert my Array with objects inside into an array of arrays. But each time either it doesn't work or my new create array is empty. For exemple, I can't run into it with angular.foreach()or myArray.forEach(function(element)). I really don't know why, because it is declared and initialized as an array "var myArray = [];".

Then I'm using pushto populate it, and what I obtained in the console.log is : enter image description here

What I would like to have is something usable into _.SortBy(myArray, 'date'); (Underscore.js) to do it I need myArray to be like that :

var stooges = [{name: 'moe', age: 40}, {name: 'larry', age: 50}, {name: 'curly', age: 60}];

Following this post : Converting a JS object to an array I tried barely everything listed, but nothing worked...Can you give me a hand ?

$scope.myArray = []; 
 
// This is into an angular.foreach
 
 $scope.myArray.push({'Name' : snapshot.val().name, 'Link' : snapshot.val().link, 'second' : snapshot.val().second, 'var' : snapshot.val().var, 'selfID' : 'MyID', 'date' : snapshot.val().date, 'hour' : snapshot.val().hour });
 
 console.log("MyArray :", $scope.myArray);

EDIT :

It seems there is a problem with my array, i tried this two function, but i couldn't see any console.log()... Is it possible that my array is locked ? Maybe no function (sort, _.sortBy...) can run into it ?

// One try to run into and sort
$scope.myArray.sort(function(x, y)
{
            var date1 = new Date(x.date);
            var date2 = new Date(y.date);
            
            console.log("My dates :", date1);
            console.log("My dates :", date2);
            
            return date1 - date2 ;
});
        
// An other try to run into my array
for(var i = 0; i < $scope.myArray.length; i++)
{
            console.log("I'm in "+i+" with : "+$scope.newCal[i]);
}

// Even is showing the complete Array in console.log is working, this below shows me "undefined"

 console.log("One element : "+$scope.newCal[1]);
Bugs
  • 4,491
  • 9
  • 32
  • 41
Memphis
  • 410
  • 5
  • 23
  • What is your desired output? An array of values? keys? [Key,values] ? – Jonas Wilms Jul 06 '17 at 09:05
  • And sortBy should work already: console.log(_.SortBy(myArray, 'date')) – Jonas Wilms Jul 06 '17 at 09:07
  • Yes indeed, it should already work ! It's none sense, when I'm trying _.SortBy(stooges, 'age')), it is actually working. But I got an empty array, when I'm injecting "myArray"... And yes, I would like an array of 'key' : value. `Push` will populate the main Array, with other Arrays, am I clear enought ? – Memphis Jul 06 '17 at 09:14
  • Solutions should be posted as answers not as updates to your question. This is to help avoid confusion. Thank you. – Bugs Jul 07 '17 at 08:12

4 Answers4

2

Like others have pointed out, you should be able to do just _.sortBy(myArray, 'date') to sort your array by date.

If all else fails you could always try myArray.sort((a, b) => a.date < b.date)

mTv
  • 1,074
  • 17
  • 28
  • Thank you, this is working very well ! But I'm trying to have my `date` from the most recent to the futur one. I tried `var myReverse = myArray.sort((a, b) => a.date < b.date)`, and in the `console.log("myReverse", myReverse.reverse());`, but i've got the same result, same order i mean, how can i change the order ? – Memphis Jul 06 '17 at 10:03
  • Try `myArray.sort((a, b) => a.date > b.date)`. Just flipped the `<`. the .sort() function can take a custom sort function as an argument( think the default is (a, b) => a – mTv Jul 06 '17 at 14:56
  • Wierd that `.reverse()` didn't work though. Side note, remember that both `.sort()` and `.reverse()` mutates the original array. So by doing `let myReverse = myArray.sort((a, b) => a.date < b.date)` and then `myReverse.reverse()` you will first sort `myArray` then you assign myArray to the myReverse variable and then reverse both myArray and myReverse since they point to the same array. – mTv Jul 06 '17 at 15:55
0

In array you used date keyword and in this function _.SortBy(myArray, 'Date'); you are sorting using Date keyword.

Replace 'Date' keyword with 'date' and then try to sort your array.

So your sort function will be _.sortBy(myArray, 'date')

Nilesh Khisadiya
  • 1,560
  • 2
  • 15
  • 27
0

You can use _.SortBy(myArray, 'date') , however as your "date" property seems to be a string literal, it will be sorted as alphabetically. You need to do something like this

function parseYourDateToRealDateObjectSomehow(dateString){  return new Date(); }

_.SortBy(myArray, function(item1,item2){  
  var bigger = parseYourDateToRealDateObjectSomehow(item1.date)  > parseYourDateToRealDateObjectSomehow(item2.date);
   if(bigger) return 1;

var smaller = parseYourDateToRealDateObjectSomehow(item1.date) < 
   parseYourDateToRealDateObjectSomehow(item2.date);
   if(smaller) return -1;

   return 0;


});
  • 1
    return +new Date(dateString+"T00:00") – Jonas Wilms Jul 06 '17 at 10:32
  • Arf, no it doesn't work actually...I d'ont understand why, because when i'm using a simply increment from 1 to 6 for exemple, the sort is successful. But when i'm trying with date, or timestamp, it's a mess. But timestamps are numeric values, right ? – Memphis Jul 07 '17 at 06:49
0

Problem solved ! So the "sort()" method was not the cause. The problem was with the asynchronous communication between firebase and my Array, when I put the sort() method inside my firebase request (the one I used to populate the Array) the sort was successful.

Thank you all for your support ! ;)

Memphis
  • 410
  • 5
  • 23