Convert an array of IsoDate to a Date Format [duplicate] - javascript

This question already has answers here:
How do I format a date in JavaScript?
(68 answers)
Closed 4 years ago.
I have an array of date
["Mon Jul 16 2018 11:40:28 GMT+0200 (CEST)",
"Fri Jul 13 2018 09:33:46 GMT+0200 (CEST)",
"Fri Jul 13 2018 09:21:36 GMT+0200 (CEST)",
"Fri Jul 13 2018 09:03:42 GMT+0200 (CEST)",
"Fri Jul 13 2018 09:01:05 GMT+0200 (CEST)",
"Fri Jul 13 2018 08:53:23 GMT+0200 (CEST)",
"Fri Jul 13 2018 08:52:33 GMT+0200 (CEST)",
"Thu Jul 12 2018 13:41:59 GMT+0200 (CEST)",
"Thu Jul 12 2018 13:41:49 GMT+0200 (CEST)",
"Thu Jul 12 2018 13:41:42 GMT+0200 (CEST)"]
I want to get it in a different format as exemple
Monday 16 July 2018 instead of "Mon Jul 16 2018 11:40:28 GMT+0200 (CEST)"
Is there a way to go through the table and transform the date ?
const test1 = test.map(a => a.toISOString().slice(0, 10))
console.log(test1)
Tried this got an error
var test = ["Mon Jul 16 2018 11:40:28 GMT+0200 (CEST)",
"Fri Jul 13 2018 09:33:46 GMT+0200 (CEST)",
"Fri Jul 13 2018 09:21:36 GMT+0200 (CEST)",
"Fri Jul 13 2018 09:03:42 GMT+0200 (CEST)",
"Fri Jul 13 2018 09:01:05 GMT+0200 (CEST)",
"Fri Jul 13 2018 08:53:23 GMT+0200 (CEST)",
"Fri Jul 13 2018 08:52:33 GMT+0200 (CEST)",
"Thu Jul 12 2018 13:41:59 GMT+0200 (CEST)",
"Thu Jul 12 2018 13:41:49 GMT+0200 (CEST)",
"Thu Jul 12 2018 13:41:42 GMT+0200 (CEST)"
]
// Monday 16 July 2018 instead of "Mon Jul 16 2018 11:40:28 GMT+0200 (CEST)"
const test1 = test.map(a => a.toISOString().slice(0, 10));
console.log(test1)

toISOString does not give me the format you wanted.
You wanted
Monday 16 July 2018
instead of
"Mon Jul 16 2018 11:40:28 GMT+0200 (CEST)"
toLocaleString gives us a long month:
new Date(a).toLocaleString("en-us",{
weekday: 'long',
year: 'numeric',
month: 'long',
day: 'numeric' })
then you can move the texts around like this:
var test = ["Mon Jul 16 2018 11:40:28 GMT+0200 (CEST)", "Fri Jul 13 2018 09:33:46 GMT+0200 (CEST)", "Fri Jul 13 2018 09:21:36 GMT+0200 (CEST)", "Fri Jul 13 2018 09:03:42 GMT+0200 (CEST)", "Fri Jul 13 2018 09:01:05 GMT+0200 (CEST)", "Fri Jul 13 2018 08:53:23 GMT+0200 (CEST)", "Fri Jul 13 2018 08:52:33 GMT+0200 (CEST)", "Thu Jul 12 2018 13:41:59 GMT+0200 (CEST)", "Thu Jul 12 2018 13:41:49 GMT+0200 (CEST)", "Thu Jul 12 2018 13:41:42 GMT+0200 (CEST)" ]
const locale = "en-us";
var options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' }
let test1 = test.map(function(a) {
let dateStr = new Date(a).toLocaleString(locale,options);
return dateStr.replace(/(\w+), (\w+) (\d+), (\d+)/,"$1 $3 $2 $4");
})
console.log(test1)
// simpler if no replace of month:
// String
test1 = test.map(a => a.split(/ \d\d:/)[0]);
console.log(test1)
// date
test1 = test.map(a => new Date(a).toDateString());
console.log(test1)

Try this.
var days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
var test = ["Mon Jul 16 2018 11:40:28 GMT+0200 (CEST)",
"Fri Jul 13 2018 09:33:46 GMT+0200 (CEST)",
"Fri Jul 13 2018 09:21:36 GMT+0200 (CEST)",
"Fri Jul 13 2018 09:03:42 GMT+0200 (CEST)",
"Fri Jul 13 2018 09:01:05 GMT+0200 (CEST)",
"Fri Jul 13 2018 08:53:23 GMT+0200 (CEST)",
"Fri Jul 13 2018 08:52:33 GMT+0200 (CEST)",
"Thu Jul 12 2018 13:41:59 GMT+0200 (CEST)",
"Thu Jul 12 2018 13:41:49 GMT+0200 (CEST)",
"Thu Jul 12 2018 13:41:42 GMT+0200 (CEST)"
]
// Monday 16 July 2018 instead of "Mon Jul 16 2018 11:40:28 GMT+0200 (CEST)"
const test1 = test.map(a => {
a= new Date(a);
var c= days[a.getDay()]+" "+a.getDate()+" "+months[a.getMonth()]+" "+a.getFullYear();
return c;
});
console.log(test1)

Related

Need exactly _d value of moment-react

I need the exact value of _d in moment-react.
_d: Wed Aug 10 2022 16:34:56 GMT+0530 (India Standard Time) {}
_isAMomentObject: true
_isUTC: false
_isValid: true
When doing moment.toString(), I am getting "Wed Aug 10 2022 16:34:56 GMT+0530" but I need "Wed Aug 10 2022 16:34:56 GMT+0530 (India Standard Time)"

Subtracting months from current date is returning unexpected results. Same Month (March) is being returned twice

I have created a method that returns the past months according to the current date and how many months from the past you need, seems like the javascript date object is
behaving wrongly.
I guess the problem has only popped up today as it's the 29th of the current month and subtracting 1 month from 29 March which is supposed to be 29 February (doesn't exist) lead javascript to fallback to +1 day...
Can someone please suggest a great fix for this.
Thanks 🙌
export type Config = {
n: number;
i18n: i18n;
};
export default function NMonthsAgo(config: Config): MonthsAgo[] {
const date = new Date();
const { i18n } = config;
let { n } = config;
n = Math.max(n, 0);
// add one month in future to include the current one.
date.setMonth(date.getMonth() + 1);
return Array.from({ length: n || 1 })
.map(() => {
date.setMonth(date.getMonth() - 1);
console.log(date.toDateString()); ===> console print.
return {
monthName: formatDate(i18n, date, { month: 'long' }),
monthNameShort: formatDate(i18n, date, { month: 'short' }),
year: formatDate(i18n, date, { year: 'numeric' }),
};
})
.reverse();
}
Output:
NMonthsAgo.js:36 Wed Dec 29 2021
NMonthsAgo.js:36 Mon Nov 29 2021
NMonthsAgo.js:36 Fri Oct 29 2021
NMonthsAgo.js:36 Wed Sep 29 2021
NMonthsAgo.js:36 Sun Aug 29 2021
NMonthsAgo.js:36 Thu Jul 29 2021
NMonthsAgo.js:36 Tue Jun 29 2021
NMonthsAgo.js:36 Sat May 29 2021
NMonthsAgo.js:36 Thu Apr 29 2021
NMonthsAgo.js:36 Mon Mar 29 2021 <=== Mars is returned twice...
NMonthsAgo.js:36 Mon Mar 01 2021 <=== Mars is returned twice...
NMonthsAgo.js:36 Mon Feb 01 2021
NMonthsAgo.js:36 Fri Jan 01 2021
NMonthsAgo.js:36 Wed Dec 29 2021

javascript string date from array and group by day and return new array per day

I got a array of data the data is dates they are sorted per day.
Wat i want to get is the data grouped by date(day) and all the data of every day needs to be in new and separated array. my data can be short 1 date string in 1 day or very long month's or year
My data:
accountDateArray = [
Mon Jun 08 2020 19:47:16 GMT+0200 (Midden-Europese zomertijd),
Mon Jun 08 2020 19:47:26 GMT+0200 (Midden-Europese zomertijd),
Mon Jun 08 2020 19:47:34 GMT+0200 (Midden-Europese zomertijd),
Tue Jun 09 2020 15:40:31 GMT+0200 (Midden-Europese zomertijd),
Tue Jun 09 2020 15:42:28 GMT+0200 (Midden-Europese zomertijd),
Wed Jun 10 2020 00:06:50 GMT+0200 (Midden-Europese zomertijd),
Wed Jun 10 2020 00:06:50 GMT+0200 (Midden-Europese zomertijd),
Wed Jun 10 2020 08:10:51 GMT+0200 (Midden-Europese zomertijd),
Fri Jun 12 2020 10:59:21 GMT+0200 (Midden-Europese zomertijd),
...
]
What i want is:
[
[
Mon Jun 08 2020 19:47:16 GMT+0200 (Midden-Europese zomertijd),
Mon Jun 08 2020 19:47:26 GMT+0200 (Midden-Europese zomertijd),
Mon Jun 08 2020 19:47:34 GMT+0200 (Midden-Europese zomertijd),
],
[
Tue Jun 09 2020 15:40:31 GMT+0200 (Midden-Europese zomertijd),
Tue Jun 09 2020 15:42:28 GMT+0200 (Midden-Europese zomertijd),
],
...
]
My code
let NewDateArray = [];
for (let i in accountDateArray) {
NewDateArray.push(accountDateArray[i].toString().substring(0, 10));
}
let unique = [...new Set(NewDateArray)];
for (let i in accountDateArray) {
for (let n in unique) {
if (
unique[n] === accountDateArray[i].toString().substring(0, 10)
) {
console.log(accountDateArray[i]);
}
}
}
You can make use of reduce function, I hope this will lead you to the right direction.
var accountDateArray = ['Mon Jun 08 2020 19:47:16 GMT+0200 (Midden-Europese zomertijd)','Mon Jun 08 2020 19:47:26 GMT+0200 (Midden-Europese zomertijd)','Mon Jun 08 2020 19:47:34 GMT+0200 (Midden-Europese zomertijd)','Tue Jun 09 2020 15:40:31 GMT+0200 (Midden-Europese zomertijd)','Tue Jun 09 2020 15:42:28 GMT+0200 (Midden-Europese zomertijd)','Wed Jun 10 2020 00:06:50 GMT+0200 (Midden-Europese zomertijd)','Wed Jun 10 2020 00:06:50 GMT+0200 (Midden-Europese zomertijd)','Wed Jun 10 2020 08:10:51 GMT+0200 (Midden-Europese zomertijd)','Fri Jun 12 2020 10:59:21 GMT+0200 (Midden-Europese zomertijd)',];
var result = Object.values(accountDateArray.reduce((acc, date)=>{
const key = new Date(date).getDate();
acc[key] = [...(acc[key] || []), date];
return acc;
},{}));
console.log(result);

JS Filter an array of arrays

I'm trying to filter an array of arrays.
I've searched for an answer on S.O. but all questions I've come accross don't match mine (array of objects or a simple array not nested or not the same format, etc ...)
Once the values are stored in an array they look like this:
[[Paris, One ONE, Boss, Wed Mar 01 00:00:00 GMT+01:00 2017, ], [Paris, Two TWO, Temp, Sat Jul 01 00:00:00 GMT+02:00 2017, ], [Paris, Three THREE, Employee, Sat Sep 01 00:00:00 GMT+02:00 2018, ], [Paris, Four FOUR, Intern, Thu Nov 01 00:00:00 GMT+01:00 2018, ], [Paris, Five FIVE, N.A., Sat Dec 01 00:00:00 GMT+01:00 2018, ], [Paris, Six SIX, Director, Tue Jan 01 00:00:00 GMT+01:00 2019, ], [Paris, Seven SEVEN, Director, Fri Jan 01 00:00:00 GMT+01:00 2016, Sun Jul 01 00:00:00 GMT+02:00 2018], [Paris, Eight EIGHT, Director, Fri Jan 01 00:00:00 GMT+01:00 2016, Sun Oct 01 00:00:00 GMT+02:00 2017], [Paris, Nine NINE, N.A., Thu Nov 01 00:00:00 GMT+01:00 2018, Sat Dec 01 00:00:00 GMT+01:00 2018], [London, Ten TEN, Employee, Fri Jan 01 00:00:00 GMT+01:00 2016, Mon Oct 01 00:00:00 GMT+02:00 2018], [London, Eleven ELEVEN, Intern, Mon Feb 01 00:00:00 GMT+01:00 2016, Mon Jan 01 00:00:00 GMT+01:00 2018], [London, Twelve TWELVE, Employee, Sun May 01 00:00:00 GMT+02:00 2016, Sun Oct 01 00:00:00 GMT+02:00 2017]]
I would like to be able to filter this array of arrays and keep the data linked to one community only, Paris for instance.
How would I do that?
Thanks a lot for your help
you can use Array.filter, somthing like this:
const data = [
['Paris', 'One ONE', 'Boss', 'Wed Mar 01 00:00:00 GMT+01:00 2017' ],
['Paris', 'Two TWO', 'Temp', 'Sat Jul 01 00:00:00 GMT+02:00 2017' ],
['London', 'Three THREE', 'Employee, Sat Sep 01 00:00:00 GMT+02:00 2018' ]];
const result = data.filter(function(item) { return item[0]==='Paris'});
// const result = data.filter(item => item[0]==='Paris'); // ES6
console.log(result);
Assuming all of the elements in your arrays are Strings, you just check that each array in your array of arrays contains the element Paris.
const yourArray = [
['Paris', 'one One', 'whatever else'],
['Paris', 'one One', 'whatever else'],
['Paris', 'one One', 'whatever else'],
['Paris', 'one One', 'whatever else'],
['London', 'one One', 'whatever else'],
]
const onlyParis = yourArray.filter(function(array) {
return array.includes('Paris')
})
console.log(onlyParis)
I have converted your array to a JavaScript array. And the filter will be done like this:
var myArray = [['Paris', 'One ONE', 'Boss', 'Wed Mar 01 00:00:00 GMT+01:00 2017'],['Paris', 'Two TWO', 'Temp', 'Sat Jul 01 00:00:00 GMT+02:00 2017'],['Paris', 'Three THREE', 'Employee', 'Sat Sep 01 00:00:00 GMT+02:00 2018'],['Paris', 'Four FOUR', 'Intern', 'Thu Nov 01 00:00:00 GMT+01:00 2018'],['Paris', 'Five FIVE', 'N.A.', 'Sat Dec 01 00:00:00 GMT+01:00 2018'],['Paris', 'Six SIX', 'Director', 'Tue Jan 01 00:00:00 GMT+01:00 2019'],['Paris', 'Seven SEVEN', 'Director', 'Fri Jan 01 00:00:00 GMT+01:00 2016', 'Sun Jul 01 00:00:00 GMT+02:00 2018'],['Paris', 'Eight EIGHT', 'Director', 'Fri Jan 01 00:00:00 GMT+01:00 2016', 'Sun Oct 01 00:00:00 GMT+02:00 2017'],['Paris', 'Nine NINE', 'N.A.', 'Thu Nov 01 00:00:00 GMT+01:00 2018', 'Sat Dec 01 00:00:00 GMT+01:00 2018'],['London', 'Ten TEN', 'Employee', 'Fri Jan 01 00:00:00 GMT+01:00 2016', 'Mon Oct 01 00:00:00 GMT+02:00 2018'],['London', 'Eleven ELEVEN', 'Intern', 'Mon Feb 01 00:00:00 GMT+01:00 2016', 'Mon Jan 01 00:00:00 GMT+01:00 2018'],['London', 'Twelve TWELVE', 'Employee', 'Sun May 01 00:00:00 GMT+02:00 2016', 'Sun Oct 01 00:00:00 GMT+02:00 2017']]
var filteredArray = myArray.filter(function (item){
return item[0] == 'Paris'
})
console.log(filteredArray)
This function filters the elements of lst which are lists. It compares each list's first element (which is the city name in your example) and returns true if and only if it is equal to 'Paris'.
This makes the implicit assumption that the structure of innerLst does not change. If the city name is moved to a different index, then larz's solution accounts for that.
lst.filter(innerLst => innerLst[0] === 'Paris')
const country = [
["Paris", "One ONE, Boss, Wed Mar 01 00: 00: 00 GMT + 01: 00 2017", ],
["Paris", "Two TWO, Temp, Sat Jul 01 00: 00: 00 GMT + 02: 00 2017", ],
["Paris", "Three THREE, Employee, Sat Sep 01 00: 00: 00 GMT + 02: 00 2018", ],
["Paris", "Four FOUR, Intern, Thu Nov 01 00: 00: 00 GMT + 01: 00 2018", ],
["Paris", "Five FIVE, N.A., Sat Dec 01 00: 00: 00 GMT + 01: 00 2018", ],
["Paris, Seven SEVEN, Director, Fri Jan 01 00: 00: 00 GMT + 01: 00 2016, Sun Jul 01 00: 00: 00 GMT + 02: 00 2018"],
["London", "Twelve TWELVE, Employee, Sun May 01 00: 00: 00 GMT + 02: 00 2016", "Sun Oct 01 00: 00: 00 GMT + 02: 00 2017"]
]
const res = country.filter(([ville, b, c]) => ville === "Paris")
console.log(res)

How to create array of object in angularjs or javascript

I have below array and I want to create another array of array. below is my array
$scope.data2 =
[
{"dt":"07 Jul 2015","avgdelay":"10","code_sent_time":"07 Jul 2015 12:30 PM","reply_received_time":"07 Jul 2015 12:40 PM","time_diff":10},
{"dt":"07 Jul 2015","avgdelay":"10","code_sent_time":"07 Jul 2015 02:10 AM","reply_received_time":"07 Jul 2015 02:30 AM","time_diff":20 },
{"dt":"07 Jul 2015","avgdelay":"10","code_sent_time":"07 Jul 2015 03:10 AM","reply_received_time":"07 Jul 2015 03:15 AM","time_diff":5 },
{"dt":"07 Jul 2015","avgdelay":"10","code_sent_time":"07 Jul 2015 04:45 AM","reply_received_time":"07 Jul 2015 05:00 AM","time_diff":15 },
{"dt":"08 Jul 2015","avgdelay":"20","code_sent_time":"08 Jul 2015 12:30 PM","reply_received_time":"08 Jul 2015 12:40 PM","time_diff":35},
{"dt":"08 Jul 2015","avgdelay":"20","code_sent_time":"08 Jul 2015 02:10 AM","reply_received_time":"08 Jul 2015 02:30 AM","time_diff":42 },
{"dt":"08 Jul 2015","avgdelay":"20","code_sent_time":"08 Jul 2015 03:10 AM","reply_received_time":"08 Jul 2015 03:15 AM","time_diff":5 },
{"dt":"08 Jul 2015","avgdelay":"20","code_sent_time":"08 Jul 2015 04:45 AM","reply_received_time":"08 Jul 2015 05:00 AM","time_diff":5 },
{"dt":"09 Jul 2015","avgdelay":"30","code_sent_time":"09 Jul 2015 12:30 PM","reply_received_time":"09 Jul 2015 12:40 PM","time_diff":1},
{"dt":"09 Jul 2015","avgdelay":"30","code_sent_time":"09 Jul 2015 02:10 AM","reply_received_time":"09 Jul 2015 02:30 AM","time_diff":28},
{"dt":"09 Jul 2015","avgdelay":"30","code_sent_time":"09 Jul 2015 03:10 AM","reply_received_time":"09 Jul 2015 03:15 AM","time_diff":12},
{"dt":"09 Jul 2015","avgdelay":"30","code_sent_time":"09 Jul 2015 04:45 AM","reply_received_time":"09 Jul 2015 05:00 AM","time_diff":17},
{"dt":"10 Jul 2015","avgdelay":"10","code_sent_time":"10 Jul 2015 12:30 PM","reply_received_time":"10 Jul 2015 12:40 PM","time_diff":19},
{"dt":"10 Jul 2015","avgdelay":"10","code_sent_time":"10 Jul 2015 02:10 AM","reply_received_time":"10 Jul 2015 02:30 AM","time_diff":21},
{"dt":"10 Jul 2015","avgdelay":"10","code_sent_time":"10 Jul 2015 03:10 AM","reply_received_time":"10 Jul 2015 03:15 AM","time_diff":15},
{"dt":"10 Jul 2015","avgdelay":"10","code_sent_time":"10 Jul 2015 04:45 AM","reply_received_time":"10 Jul 2015 05:00 AM","time_diff":15},
{"dt":"11 Jul 2015","avgdelay":"10", "code_sent_time":"11 Jul 2015 12:30 PM","reply_received_time":"11 Jul 2015 12:40 PM","time_diff":39},
{"dt":"11 Jul 2015","avgdelay":"10", "code_sent_time":"11 Jul 2015 02:10 AM","reply_received_time":"11 Jul 2015 02:30 AM","time_diff":7},
{"dt":"11 Jul 2015","avgdelay":"10", "code_sent_time":"11 Jul 2015 03:10 AM","reply_received_time":"11 Jul 2015 03:15 AM","time_diff":9},
{"dt":"11 Jul 2015","avgdelay":"10", "code_sent_time":"11 Jul 2015 04:45 AM","reply_received_time":"11 Jul 2015 05:00 AM","time_diff":22},
{"dt":"12 Jul 2015","avgdelay":"10","code_sent_time":"12 Jul 2015 12:30 PM","reply_received_time":"12 Jul 2015 12:40 PM","time_diff":32},
{"dt":"12 Jul 2015","avgdelay":"10","code_sent_time":"12 Jul 2015 02:10 AM","reply_received_time":"12 Jul 2015 02:30 AM","time_diff":11},
{"dt":"12 Jul 2015","avgdelay":"10","code_sent_time":"12 Jul 2015 03:10 AM","reply_received_time":"12 Jul 2015 03:15 AM","time_diff":52},
{"dt":"12 Jul 2015","avgdelay":"10","code_sent_time":"12 Jul 2015 04:45 AM","reply_received_time":"12 Jul 2015 05:00 AM","time_diff":37}
];
My resultant array would be like,
$scope.resarray = [
{"dt":"07 Jul 2015","avgdelay":"10","data" :
[
{"code_sent_time":"07 Jul 2015 12:30 PM","reply_received_time":"07 Jul 2015 12:40 PM","time_diff":10},
{"code_sent_time":"07 Jul 2015 02:10 AM","reply_received_time":"07 Jul 2015 02:30 AM","time_diff":20 },
{"code_sent_time":"07 Jul 2015 03:10 AM","reply_received_time":"07 Jul 2015 03:15 AM","time_diff":5 },
{"code_sent_time":"07 Jul 2015 04:45 AM","reply_received_time":"07 Jul 2015 05:00 AM","time_diff":15 }
]
},
{"dt":"08 Jul 2015","avgdelay":"20","data":
[
{"code_sent_time":"08 Jul 2015 12:30 PM","reply_received_time":"08 Jul 2015 12:40 PM","time_diff":35},
{"code_sent_time":"08 Jul 2015 02:10 AM","reply_received_time":"08 Jul 2015 02:30 AM","time_diff":42 },
{"code_sent_time":"08 Jul 2015 03:10 AM","reply_received_time":"08 Jul 2015 03:15 AM","time_diff":5 },
{"code_sent_time":"08 Jul 2015 04:45 AM","reply_received_time":"08 Jul 2015 05:00 AM","time_diff":5 }
]
},
{"dt":"09 Jul 2015","avgdelay":"30","data":
[
{"code_sent_time":"09 Jul 2015 12:30 PM","reply_received_time":"09 Jul 2015 12:40 PM","time_diff":1},
{"code_sent_time":"09 Jul 2015 02:10 AM","reply_received_time":"09 Jul 2015 02:30 AM","time_diff":28},
{"code_sent_time":"09 Jul 2015 03:10 AM","reply_received_time":"09 Jul 2015 03:15 AM","time_diff":12},
{"code_sent_time":"09 Jul 2015 04:45 AM","reply_received_time":"09 Jul 2015 05:00 AM","time_diff":17},
]
}
];
Every date have 4 rows and I want to generate the array where every date have another array having all 4 details for that date.
I tried with various options but not successded. Kindly help me to solve this.
Below Is what I have tried,
$scope.genarr = function()
{
var data2length = $scope.data2.length;
var firstdate = $scope.data2[0].dt;
var sourcecheckdate = firstdate.split(' ');
$scope.resarray = {};
var dtcheckflag = false;
var insertrow = null;
var j = 0;
var k = 0;
var z = 1;
var data=[];
for (var i=0;i< data2length;i++)
{
var targetcheckdate = $scope.data2[i].code_sent_time.split(' ');
if (targetcheckdate[0] === sourcecheckdate[0] && targetcheckdate[1] === sourcecheckdate[1] && targetcheckdate[2] === sourcecheckdate[2])
{
firstdate = $scope.data2[i].dt;
sourcecheckdate = firstdate.split(' ');
}
else
{
firstdate = $scope.data2[i].dt;
sourcecheckdate = firstdate.split(' ');
}
if (insertrow != firstdate)
{
k = 0;
$scope.resarray[j] = {"dt":$scope.data2[i].dt,"avgdelay":$scope.data2[i].avgdelay,"data":[]};
//$scope.resarray[j].data[k] = null;
$scope.resarray[j].data[k]= {"code_sent_time":$scope.data2[i].code_sent_time , "reply_received_time" :$scope.data2[i].reply_received_time , "time_diff" : $scope.data2[i].time_diff};
insertrow = firstdate;
j+=1;
}
else
{
$scope.resarray[j].data[k]= {"code_sent_time":$scope.data2[i].code_sent_time , "reply_received_time" :$scope.data2[i].reply_received_time , "time_diff" : $scope.data2[i].time_diff};
}
k +=1;
}
};
Please check this : http://plnkr.co/edit/BOYqNcdpi8uGKRraDAw1?p=preview
Controller:
$scope.data = []; //New formated array of data
for(var k = 0; k < $scope.data2.length; k++) {
var arrayData = {}; //Temporary object
arrayData.dt = $scope.data2[k].dt;
arrayData.avgdelay = $scope.data2[k].avgdelay;
arrayData.data = [];
for(var j = 0; j<4; j++,k++) {
if (arrayData.dt == $scope.data2[k].dt) {
var tempObj = {'code_sent_time': $scope.data2[k].code_sent_time,
'reply_received_time':$scope.data2[k].reply_received_time,
'time_diff':$scope.data2[k].time_diff
};
arrayData.data.push(tempObj);
}
}
$scope.data.push(arrayData);
}
the data field is currently an object, but it needs to be an array.
{"dt":"07 Jul 2015","avgdelay":"10","data" :
{
{"code_sent_time":"07 Jul 2015 12:30 PM"},
{"code_sent_time":"07 Jul 2015 02:10 AM"},
{"code_sent_time":"07 Jul 2015 03:10 AM"},
{"code_sent_time":"07 Jul 2015 04:45 AM"}
}
},
should be
{"dt":"07 Jul 2015","avgdelay":"10","data" :
[
{"code_sent_time":"07 Jul 2015 12:30 PM"},
{"code_sent_time":"07 Jul 2015 02:10 AM"},
{"code_sent_time":"07 Jul 2015 03:10 AM"},
{"code_sent_time":"07 Jul 2015 04:45 AM"}
]
},
{} creates and object, [] creates and array
var newData = [];
var dateDict = {};
angular.forEach(data2,function(d){
var temp = {};
if(dateDict[d.dt]){
dateDict[d.dt].push(d);
}
else{
dateDict[d.dt] = [];
dateDict[d.dt].push(d);
}
});
angular.forEach(dateDict,function(val,key){
var obj = {
"dt":key,
"avgdelay":val[0].avgdelay,
"data": val
};
newData.push(obj);
});
I think this will solve your problem. data2 is old data, newData is the one that you want.

Categories