How to group json object dates by month? - javascript

I have a JSON object like so:
{
"Sat Jul 28 2018 03:36:36 GMT-0700 (Pacific Daylight Time)":[
{ ... },
{ ... }
],
"Fri Aug 03 2018 19:07:14 GMT-0700 (Pacific Daylight Time)":[
{ ... }
],
"Sat Aug 18 2018 17:25:50 GMT-0700 (Pacific Daylight Time)":[
{ ... }
]
}
How can I return this into a grouped object by month?

Try this (with lodash):
var input = {
"Sat Jul 28 2018 03:36:36 GMT-0700 (Pacific Daylight Time)": [
"test", "tester"
],
"Fri Aug 03 2018 19:07:14 GMT-0700 (Pacific Daylight Time)": [
"tester1"
],
"Sat Aug 18 2018 17:25:50 GMT-0700 (Pacific Daylight Time)": [
"tester2"
]
}
var groupedKeys = _.groupBy(Object.keys(input), function(key) {
return key.substring(4, 7)
});
var groupedEntries = _.map(groupedKeys, (values, key) => {
values = _.map(values, (value) => {
return input[value]
});
return {
key: key,
values: _.flattenDeep(values)
};
});
var result = _.reduce(groupedEntries, (result, obj) => {
result[obj.key] = obj.values;
return result;
}, {})
console.log(result);
Output:
{
"Jul": [
"test",
"tester"
],
"Aug": [
"tester1",
"tester2"
]
}

Loop through the object then extract month and create new object:
let jsonObj = {
"Sat Jul 28 2018 03:36:36 GMT-0700 (Pacific Daylight Time)":[{},{}],
"Fri Aug 03 2018 19:07:14 GMT-0700 (Pacific Daylight Time)":[{}],
"Sat Aug 18 2018 17:25:50 GMT-0700 (Pacific Daylight Time)":[{}]
};
let grouped = Object.keys(jsonObj).reduce((prev,key,i,all)=>{
let monthArr = {};
if(typeof prev === 'string'){
monthArr[prev.substr(4,3)] = jsonObj[prev];
prev = {};
}
monthArr[key.substr(4,3)] = jsonObj[key];
return {...prev,...monthArr};
});

Related

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);

Finding all object indices satisfying testing function, like array.findIndex() but more than first index

I have an array of objects:
var data = [{"district":"201","date":"Wed Apr 01 2020","paper":671.24,"mgp":36.5},
{"district":"202","date":"Wed Apr 01 2020","paper":421.89,"mgp":44.2},
{"district":"203","date":"Wed Apr 01 2020","paper":607.85,"mgp":67.36},
{"district":"201","date":"Sun Mar 01 2020","paper":571.24,"mgp":38.8},
{"district":"202","date":"Sun Mar 01 2020","paper":421.89,"mgp":36.6},
{"district":"203","date":"Sun Mar 01 2020","paper":607.85,"mgp":69.36},
{"district":"201","date":"Sat Feb 01 2020","paper":571.24,"mgp":38.8},
{"district":"202","date":"Sat Feb 01 2020","paper":421.89,"mgp":22.2},
{"district":"203","date":"Sat Feb 01 2020","paper":607.85,"mgp":59.66},
{"district":"201","date":"Wed Jan 01 2020","paper":571.24,"mgp":38.8},
{"district":"202","date":"Wed Jan 01 2020","paper":421.89,"mgp":22.2},
{"district":"203","date":"Wed Jan 01 2020","paper":607.85,"mgp":89.26}]
For each district I would like to take the values of paper and mgp for February, March, and April and divide each month's values by their January values to measure % change.
Using Array.findIndex() and reduce() (like here) I can match the districts and return the correct value, but that only returns the first index of the match, and there are repeat indices because there are subsequent months. I've tried wrapping a loop of months around the findIndex() to no avail: console.log(result) returns nothing
var result = data.reduce((a, b) => {
var months = [...new Set(a.map(m => a.date))];
for (var month of months){
var idx = a.findIndex(e => e.district == b.district && e.date == month)
if (~idx && b.date === "Wed Jan 01 2020") {
a[idx].paper = a[idx].paper/b.paper;
a[idx].mgp = a[idx].mgp/b.mgp}
else {
a.push(JSON.parse(JSON.stringify(b)));
}
return a
}, []);
Result should look like this:
[{"district":"201","date":"Wed Apr 01 2020","paper":1.17,"mgp":0.94},
{"district":"202","date":"Wed Apr 01 2020","paper":1,"mgp":1.99},
{"district":"203","date":"Wed Apr 01 2020","paper":1,"mgp":0.75},
{"district":"201","date":"Sun Mar 01 2020","paper":1,"mgp":1},
{"district":"202","date":"Sun Mar 01 2020","paper":1,"mgp":1.64},
{"district":"203","date":"Sun Mar 01 2020","paper":1,"mgp":0.77},
{"district":"201","date":"Sat Feb 01 2020","paper":1.17,"mgp":1},
{"district":"202","date":"Sat Feb 01 2020","paper":1,"mgp":1},
{"district":"203","date":"Sat Feb 01 2020","paper":1,"mgp":0.67]
var data = [{"district":"201","date":"Wed Apr 01 2020","paper":671.24,"mgp":36.5},
{"district":"202","date":"Wed Apr 01 2020","paper":421.89,"mgp":44.2},
{"district":"203","date":"Wed Apr 01 2020","paper":607.85,"mgp":67.36},
{"district":"201","date":"Sun Mar 01 2020","paper":571.24,"mgp":38.8},
{"district":"202","date":"Sun Mar 01 2020","paper":421.89,"mgp":36.6},
{"district":"203","date":"Sun Mar 01 2020","paper":607.85,"mgp":69.36},
{"district":"201","date":"Sat Feb 01 2020","paper":571.24,"mgp":38.8},
{"district":"202","date":"Sat Feb 01 2020","paper":421.89,"mgp":22.2},
{"district":"203","date":"Sat Feb 01 2020","paper":607.85,"mgp":59.66},
{"district":"201","date":"Wed Jan 01 2020","paper":571.24,"mgp":38.8},
{"district":"202","date":"Wed Jan 01 2020","paper":421.89,"mgp":22.2},
{"district":"203","date":"Wed Jan 01 2020","paper":607.85,"mgp":89.26}]
let res = data.filter(it => !it.date.includes('Jan'))
.reduce ((acc, rec) => [...acc, {district: rec.district, date: rec.date,
paper: +(rec.paper/data.filter(it => (it.district === rec.district && it.date.includes('Jan')))[0].paper).toFixed(2),
mgp: +(rec.mgp/data.filter(it => (it.district === rec.district && it.date.includes('Jan')))[0].mgp).toFixed(2)}]
,[])
console.log(JSON.stringify(res))
var data = [{"district":"201","date":"Wed Apr 01 2020","paper":671.24,"mgp":36.5},
{"district":"202","date":"Wed Apr 01 2020","paper":421.89,"mgp":44.2},
{"district":"203","date":"Wed Apr 01 2020","paper":607.85,"mgp":67.36},
{"district":"201","date":"Sun Mar 01 2020","paper":571.24,"mgp":38.8},
{"district":"202","date":"Sun Mar 01 2020","paper":421.89,"mgp":36.6},
{"district":"203","date":"Sun Mar 01 2020","paper":607.85,"mgp":69.36},
{"district":"201","date":"Sat Feb 01 2020","paper":571.24,"mgp":38.8},
{"district":"202","date":"Sat Feb 01 2020","paper":421.89,"mgp":22.2},
{"district":"203","date":"Sat Feb 01 2020","paper":607.85,"mgp":59.66},
{"district":"201","date":"Wed Jan 01 2020","paper":571.24,"mgp":38.8},
{"district":"202","date":"Wed Jan 01 2020","paper":421.89,"mgp":22.2},
{"district":"203","date":"Wed Jan 01 2020","paper":607.85,"mgp":89.26}];
const byDistrict = {};
for (const item of data) {
const month = item.date.split(' ')[1]; // eg Apr
if (!byDistrict[item.district]) byDistrict[item.district] = { };
byDistrict[item.district][month] = { paper: item.paper, mgp: item.mgp, date: item.date };
}
for (const district of Object.keys(byDistrict)) {
const District = byDistrict[district];
District.all = {
district,
paper: (District.Feb.paper + District.Mar.paper + District.Apr.paper) / District.Jan.paper,
mgp: (District.Feb.mgp + District.Mar.mgp + District.Apr.mgp) / District.Jan.mgp
}
}
const result = Object.keys(byDistrict).map(it => byDistrict[it].all);
console.log(result);
You could collect the january values first and then omit january in the result set.
var data = [{ district: "201", date: "Wed Apr 01 2020", paper: 671.24, mgp: 36.5 }, { district: "202", date: "Wed Apr 01 2020", paper: 421.89, mgp: 44.2 }, { district: "203", date: "Wed Apr 01 2020", paper: 607.85, mgp: 67.36 }, { district: "201", date: "Sun Mar 01 2020", paper: 571.24, mgp: 38.8 }, { district: "202", date: "Sun Mar 01 2020", paper: 421.89, mgp: 36.6 }, { district: "203", date: "Sun Mar 01 2020", paper: 607.85, mgp: 69.36 }, { district: "201", date: "Sat Feb 01 2020", paper: 571.24, mgp: 38.8 }, { district: "202", date: "Sat Feb 01 2020", paper: 421.89, mgp: 22.2 }, { district: "203", date: "Sat Feb 01 2020", paper: 607.85, mgp: 59.66 }, { district: "201", date: "Wed Jan 01 2020", paper: 571.24, mgp: 38.8 }, { district: "202", date: "Wed Jan 01 2020", paper: 421.89, mgp: 22.2 }, { district: "203", date: "Wed Jan 01 2020", paper: 607.85, mgp: 89.26 }],
january = data.reduce((r, o) => {
if (o.date.includes('Jan')) r[o.district] = o;
return r;
}, {}),
result = data.reduce((r, o) => {
if (o.date.includes('Jan')) return r;
r.push({
...o,
paper: +(o.paper / january[o.district].paper).toFixed(2),
mgp: +(o.mgp / january[o.district].mgp).toFixed(2)
})
return r;
}, []);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Javascript sorting with two properties

I'm struggling with sorting two levels. The logic is as follows. If any of the objects have a status, return the most recent object with a status. If none of the objects have a status, return the most recent object without a status.
var apps = [
{ status: 'PASS',
date_created: Thu Sep 03 2015 17:24:45 GMT-0700 (PDT)
},
{ status: 'FAIL',
date_created: Thu Sep 02 2015 17:24:45 GMT-0700 (PDT),
},
{ status: '',
date_created: Thu Sep 03 2015 17:24:45 GMT-0700 (PDT),
}
]
var desired_result = [{ status: 'PASS',
date_created: Thu Sep 03 2015 17:24:45 GMT-0700 (PDT)
}]
var apps_2 = [
{ status: '',
date_created: Thu Sep 03 2015 17:24:45 GMT-0700 (PDT)
},
{ status: '',
date_created: Thu Sep 02 2015 17:24:45 GMT-0700 (PDT),
},
{ status: '',
date_created: Thu Sep 01 2015 17:24:45 GMT-0700 (PDT),
}
]
var desired_resul2 = [{ status: '',
date_created: Thu Sep 03 2015 17:24:45 GMT-0700 (PDT)
}]
I've tried
var sorted = _.sort_by(apps, function (x) { x.date_updated });
I've also looked a few other SO questions but can't keep the objects in place after the first sort.
If I understand your question correctly, here is what you are looking for. http://jsfiddle.net/whxu5sea/3/
You need to filter the elements to ensure they have a status of ANY kind. Then sort them by date, earliest first. Then get that first value. In my example I assumed the dates where strings, but still should work.
var apps = [
{ status: 'PASS',
date_created: 'Thu Sep 03 2015 17:24:45 GMT-0700 (PDT)'
},
{ status: 'FAIL',
date_created: 'Thu Sep 02 2015 17:24:45 GMT-0700 (PDT)',
},
{ status: '',
date_created: 'Thu Sep 01 2015 17:24:45 GMT-0700 (PDT)',
}
];
var x;
var arr = _.filter(apps, function(data) {
return !!data.status.length;
});
x = _.chain( arr.length ? arr : apps )
.sortBy(function(data) {
return new Date(data.date_created).getTime();
}).last().value();
console.log( x );
To check if it works when no status is provided: http://jsfiddle.net/whxu5sea/4/
I hope this helps. LMK if any further clarification is needed.
EDIT: Updated to get NEWEST element (last).
Here is a simple solution that iterates just once through apps, without a filtering step. See here for a jsfiddle.
The concept is that, if status is falsy, its date is converted into a negative number that retains the correct sort order among all falsy elements, but makes all falsy elements have a lower sort order than all non-falsy ones.
We convert the falsy element dates by subtracting 8640000000000001, which is (the maximimum millis in a Date, plus one).
var x = _.chain(apps).sortBy(function(x) {
var date = new Date(x.date_created).getTime();
return x.status ? date : date - 8640000000000001;
}).last().value();
console.log( x );

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