0

I have and array of arrays like this.

var myArray = [
                [Sun Jul 24 2016 17:00:00 GMT-0500 (COT), '2', 'Text Text'],
                [Sun Jul 12 2016 17:00:00 GMT-0500 (COT), '3', 'Text Text'],
                [Mon Jul 07 2016 17:00:00 GMT-0500 (COT), '4', 'Text Text'],
                [Sun Jul 01 2016 17:00:00 GMT-0500 (COT), '1', 'Text Text'],
                [Sun Jan 01 2016 17:00:00 GMT-0500 (COT), '2', 'Text Text'],
                [Sun Jan 02 2016 17:00:00 GMT-0500 (COT), '1', 'Text Text'],
              ];

So, I want to change some items of these array according to the Dates just like this:

var myArray = [
             [Sun Jul 24 2016 17:00:00 GMT-0500 (COT), '4', 'Text Text'],
             [Sun Jul 12 2016 17:00:00 GMT-0500 (COT), '3', 'Text Text'],
             [Mon Jul 07 2016 17:00:00 GMT-0500 (COT), '2', 'Text Text'],
             [Sun Jul 01 2016 17:00:00 GMT-0500 (COT), '1', 'Text Text'],
             [Sun Jan 01 2016 17:00:00 GMT-0500 (COT), '1', 'Text Text'],
             [Sun Jan 02 2016 17:00:00 GMT-0500 (COT), '2', 'Text Text'],
                      ];

Thanks in advance.

Edit:

What's the logic of these?: Ok, it's about by replace items setting the position of the array into the main array.

For example. If the day is starting the month the number is lower like 1 or 2 and It will increase if the day of the month increse.

Jan 1 will be 1 in the second item.

Jan 2 will be 2

Jan 20 will be 3

Replace the number items.

JuanFernandoz
  • 805
  • 17
  • 40
  • @adeneo I little Mistake. Edited. – JuanFernandoz Nov 10 '16 at 22:58
  • @JeremyJStarcher I dont Want to only sorting but change item values too. – JuanFernandoz Nov 10 '16 at 22:59
  • Please read [ask]. Key phrases: "Search, and research" and "Explain ... any difficulties that have prevented you from solving it yourself". – Heretic Monkey Nov 10 '16 at 23:00
  • @blex I want to replace the number keys depending of the day in the month. – JuanFernandoz Nov 10 '16 at 23:05
  • what is the logic behind the numbers - there seems to be none except for the last 3 items where they are the day of the month ... but the first threee should be 24,12,7 according to what you just said – Jaromanda X Nov 10 '16 at 23:07
  • also, the "code" you posted is not valid javascript anyway, so you got no chance – Jaromanda X Nov 10 '16 at 23:08
  • Oh, @JaromandaX, you gave me a hint in your previous comment. JuanFernandoz, do you want the number to be an index for each month? Meaning that Jan 1st is the first date you have in Jan, Jan 2nd is the second date you have for that month, then Jul 1st is the first date you have for July, Jul 7th is the second date you have for that month and so on? – blex Nov 10 '16 at 23:10
  • 1
    @blex ... I think you've decoded it!!! – Jaromanda X Nov 10 '16 at 23:12
  • 1
    @blex, yes, please read my edit. – JuanFernandoz Nov 10 '16 at 23:12
  • @JaromandaX is valid into a Highchart poll. – JuanFernandoz Nov 10 '16 at 23:12
  • What you need is a combination of [Array#sort](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort) to sort by date, then either [Array#map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map) or [Array#forEach](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach) to fix the "index" - give it a try, and ask for any help with the code you try – Jaromanda X Nov 10 '16 at 23:15
  • `Not only sort` - you actually don't sort the array though, the start/end order of the dates is identical - you just want to change the "number", right? – Jaromanda X Nov 10 '16 at 23:17
  • that's a little more complicated, but still doable with the previously mentioned array methods – Jaromanda X Nov 10 '16 at 23:19
  • @JaromandaX Yes, Is a little complicated. – JuanFernandoz Nov 10 '16 at 23:20
  • that doesn't mean someone will write it for you - you may be lucky though – Jaromanda X Nov 10 '16 at 23:20
  • @JaromandaX what you mean? I don't want that someone write for me I just want an initial Idea. Im a lawyer sr, a lawyer in south america so give me a chance with this. – JuanFernandoz Nov 10 '16 at 23:25
  • Sorry, I thought because you haven't written any code at all ... – Jaromanda X Nov 10 '16 at 23:45

2 Answers2

0

var previous_month, position, myArray = [
  [new Date("Sun Jan 02 2016 17:00:00 GMT-0500 (COT)"), 'x', 'Text Text'],
  [new Date("Mon Jul 07 2016 17:00:00 GMT-0500 (COT)"), 'x', 'Text Text'],
  [new Date("Sun Jul 24 2016 17:00:00 GMT-0500 (COT)"), 'x', 'Text Text'],
  [new Date("Sun Jan 01 2016 17:00:00 GMT-0500 (COT)"), 'x', 'Text Text'],
  [new Date("Sun Jul 12 2016 17:00:00 GMT-0500 (COT)"), 'x', 'Text Text'],
  [new Date("Sun Jul 01 2016 17:00:00 GMT-0500 (COT)"), 'x', 'Text Text']
];

// Sort the items according to their dates in descending order
myArray.sort(function(a,b){
  return a[0] < b[0] ? 1 : (a[0] > b[0] ? -1 : 0);
});

// For each item
for(var i=myArray.length-1; i>=0; i--){
  var item = myArray[i],
      // Get the Year-Month combination
      month = item[0].getFullYear() + '-' + item[0].getMonth();
  // If it is the same as the previously encountered one
  if(month == previous_month){
    // Increase position
    position++;
  } else {
    // Otherwise, reset it
    position = 1;
    // Update previous_month
    previous_month = month;
  }
  // Insert it (converted to a String)
  item[1] = '' + position;
}

console.log(myArray);
blex
  • 24,941
  • 5
  • 39
  • 72
0

It seems you want to assign rank numbers based on the day number of the dates, ignoring the month and year. So both Jul 01 and Jan 01 would get the same rank (as in your example).

So let's create a function to get the day number from one of the sub arrays you have:

var day = o => +o[0].split(' ')[2];

Then, you don't want to sort the array itself. So for that I would suggest to take a shallow copy of your array (using the spread operator), and sort that. Then when you update the second element in one of the sub arrays, you'll still update the original array.

Here is the code:

var myArray = [
    ['Sun Jul 24 2016 17:00:00 GMT-0500 (COT)', '2', 'Text Text'],
    ['Sun Jul 12 2016 17:00:00 GMT-0500 (COT)', '3', 'Text Text'],
    ['Mon Jul 07 2016 17:00:00 GMT-0500 (COT)', '4', 'Text Text'],
    ['Sun Jul 01 2016 17:00:00 GMT-0500 (COT)', '1', 'Text Text'],
    ['Sun Jan 01 2016 17:00:00 GMT-0500 (COT)', '2', 'Text Text'],
    ['Sun Jan 02 2016 17:00:00 GMT-0500 (COT)', '1', 'Text Text'],
];
var day = o => +o[0].split(' ')[2];
[...myArray].sort( (a, b) => day(a) - day(b) )
    .reduce( (j, o, i, arr) => o[1] = j + +(!i || day(o) !== day(arr[i-1])), 0);

console.log(myArray);
.as-console-wrapper { max-height: 100% !important; top: 0; }
trincot
  • 317,000
  • 35
  • 244
  • 286