Replace into Array - Javascript -
i have , array of arrays 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, want change items of these array according dates 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 logic of these?: ok, it's replace items setting position of array main array.
for example. if day starting month number lower 1 or 2 , increase if day of month increse.
jan 1 1 in second item.
jan 2 2
jan 20 3
replace number items.
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 items according dates in descending order myarray.sort(function(a,b){ return a[0] < b[0] ? 1 : (a[0] > b[0] ? -1 : 0); }); // each item for(var i=myarray.length-1; i>=0; i--){ var item = myarray[i], // year-month combination month = item[0].getfullyear() + '-' + item[0].getmonth(); // if same encountered 1 if(month == previous_month){ // increase position position++; } else { // otherwise, reset position = 1; // update previous_month previous_month = month; } // insert (converted string) item[1] = '' + position; } console.log(myarray);
Comments
Post a Comment