Javascript Date and time - javascript

What I'm trying to achieve is to get an array of dates from a start date to an end date, along with the seconds of each particular date along with the minute in seconds so that I can easily validate if there is a way that I can work around it and it prints milliseconds instead of every hour so it's easy to validate
<script src="https://rawgit.com/moment/moment/2.2.1/min/moment.min.js"></script>
const startDateSchedule = "2020-01-08T00:00:00.000Z"
const endDateSchedule = "2020-01-12T00:00:00.000Z"
const date = function dateRange(startDate, endDate, steps = 1) {
const dateArray = [];
const currentDate = new Date(startDate);
while (currentDate <= new Date(endDate)) {
dateArray.push(new Date(currentDate));
currentDate.setUTCDate(currentDate.getUTCDate() + steps);
}
return dateArray;
}
const dates = date(startDateSchedule, endDateSchedule);
console.log(dates);
const hours = [];
for(let minute = 0; minute < 24; minute++) {
hours.push(moment({ hour }).format('h:mm A'));
}
const addItemEvery = (arr, el) =>
arr.reduce((res, v) => [...res, v, el], []);
const AllDate = addItemEvery(dates, hours);
"alldate": [
"2020-01-08T00:00:00.000Z",
[
"12:00 AM",
"1:00 AM",
"2:00 AM",
"3:00 AM",
"4:00 AM",
"5:00 AM",
"6:00 AM",
"7:00 AM",
"8:00 AM",
"9:00 AM",
"10:00 AM",
"11:00 AM",
"12:00 PM",
"1:00 PM",
"2:00 PM",
"3:00 PM",
"4:00 PM",
"5:00 PM",
"6:00 PM",
"7:00 PM",
"8:00 PM",
"9:00 PM",
"10:00 PM",
"11:00 PM"
],
"2020-01-09T00:00:00.000Z",
[
"12:00 AM",
"1:00 AM",
"2:00 AM",
"3:00 AM",
"4:00 AM",
"5:00 AM",
"6:00 AM",
"7:00 AM",
"8:00 AM",
"9:00 AM",
"10:00 AM",
"11:00 AM",
"12:00 PM",
"1:00 PM",
"2:00 PM",
"3:00 PM",
"4:00 PM",
"5:00 PM",
"6:00 PM",
"7:00 PM",
"8:00 PM",
"9:00 PM",
"10:00 PM",
"11:00 PM"
],
"2020-01-10T00:00:00.000Z",
[
"12:00 AM",
"1:00 AM",
"2:00 AM",
"3:00 AM",
"4:00 AM",
"5:00 AM",
"6:00 AM",
"7:00 AM",
"8:00 AM",
"9:00 AM",
"10:00 AM",
"11:00 AM",
"12:00 PM",
"1:00 PM",
"2:00 PM",
"3:00 PM",
"4:00 PM",
"5:00 PM",
"6:00 PM",
"7:00 PM",
"8:00 PM",
"9:00 PM",
"10:00 PM",
"11:00 PM"
],
"2020-01-11T00:00:00.000Z",
[
"12:00 AM",
"1:00 AM",
"2:00 AM",
"3:00 AM",
"4:00 AM",
"5:00 AM",
"6:00 AM",
"7:00 AM",
"8:00 AM",
"9:00 AM",
"10:00 AM",
"11:00 AM",
"12:00 PM",
"1:00 PM",
"2:00 PM",
"3:00 PM",
"4:00 PM",
"5:00 PM",
"6:00 PM",
"7:00 PM",
"8:00 PM",
"9:00 PM",
"10:00 PM",
"11:00 PM"
],
"2020-01-12T00:00:00.000Z",
[
"12:00 AM",
"1:00 AM",
"2:00 AM",
"3:00 AM",
"4:00 AM",
"5:00 AM",
"6:00 AM",
"7:00 AM",
"8:00 AM",
"9:00 AM",
"10:00 AM",
"11:00 AM",
"12:00 PM",
"1:00 PM",
"2:00 PM",
"3:00 PM",
"4:00 PM",
"5:00 PM",
"6:00 PM",
"7:00 PM",
"8:00 PM",
"9:00 PM",
"10:00 PM",
"11:00 PM"
],
"2020-01-13T00:00:00.000Z",
[
"12:00 AM",
"1:00 AM",
"2:00 AM",
"3:00 AM",
"4:00 AM",
"5:00 AM",
"6:00 AM",
"7:00 AM",
"8:00 AM",
"9:00 AM",
"10:00 AM",
"11:00 AM",
"12:00 PM",
"1:00 PM",
"2:00 PM",
"3:00 PM",
"4:00 PM",
"5:00 PM",
"6:00 PM",
"7:00 PM",
"8:00 PM",
"9:00 PM",
"10:00 PM",
"11:00 PM"
],
"2020-01-14T00:00:00.000Z",
[
"12:00 AM",
"1:00 AM",
"2:00 AM",
"3:00 AM",
"4:00 AM",
"5:00 AM",
"6:00 AM",
"7:00 AM",
"8:00 AM",
"9:00 AM",
"10:00 AM",
"11:00 AM",
"12:00 PM",
"1:00 PM",
"2:00 PM",
"3:00 PM",
"4:00 PM",
"5:00 PM",
"6:00 PM",
"7:00 PM",
"8:00 PM",
"9:00 PM",
"10:00 PM",
"11:00 PM"
],
"2020-01-15T00:00:00.000Z",
[
"12:00 AM",
"1:00 AM",
"2:00 AM",
"3:00 AM",
"4:00 AM",
"5:00 AM",
"6:00 AM",
"7:00 AM",
"8:00 AM",
"9:00 AM",
"10:00 AM",
"11:00 AM",
"12:00 PM",
"1:00 PM",
"2:00 PM",
"3:00 PM",
"4:00 PM",
"5:00 PM",
"6:00 PM",
"7:00 PM",
"8:00 PM",
"9:00 PM",
"10:00 PM",
"11:00 PM"
],
"2020-01-16T00:00:00.000Z",
[
"12:00 AM",
"1:00 AM",
"2:00 AM",
"3:00 AM",
"4:00 AM",
"5:00 AM",
"6:00 AM",
"7:00 AM",
"8:00 AM",
"9:00 AM",
"10:00 AM",
"11:00 AM",
"12:00 PM",
"1:00 PM",
"2:00 PM",
"3:00 PM",
"4:00 PM",
"5:00 PM",
"6:00 PM",
"7:00 PM",
"8:00 PM",
"9:00 PM",
"10:00 PM",
"11:00 PM"
],
"2020-01-17T00:00:00.000Z",
[
"12:00 AM",
"1:00 AM",
"2:00 AM",
"3:00 AM",
"4:00 AM",
"5:00 AM",
"6:00 AM",
"7:00 AM",
"8:00 AM",
"9:00 AM",
"10:00 AM",
"11:00 AM",
"12:00 PM",
"1:00 PM",
"2:00 PM",
"3:00 PM",
"4:00 PM",
"5:00 PM",
"6:00 PM",
"7:00 PM",
"8:00 PM",
"9:00 PM",
"10:00 PM",
"11:00 PM"
],
"2020-01-18T00:00:00.000Z",
[
"12:00 AM",
"1:00 AM",
"2:00 AM",
"3:00 AM",
"4:00 AM",
"5:00 AM",
"6:00 AM",
"7:00 AM",
"8:00 AM",
"9:00 AM",
"10:00 AM",
"11:00 AM",
"12:00 PM",
"1:00 PM",
"2:00 PM",
"3:00 PM",
"4:00 PM",
"5:00 PM",
"6:00 PM",
"7:00 PM",
"8:00 PM",
"9:00 PM",
"10:00 PM",
"11:00 PM"
],
"2020-01-19T00:00:00.000Z",
[
"12:00 AM",
"1:00 AM",
"2:00 AM",
"3:00 AM",
"4:00 AM",
"5:00 AM",
"6:00 AM",
"7:00 AM",
"8:00 AM",
"9:00 AM",
"10:00 AM",
"11:00 AM",
"12:00 PM",
"1:00 PM",
"2:00 PM",
"3:00 PM",
"4:00 PM",
"5:00 PM",
"6:00 PM",
"7:00 PM",
"8:00 PM",
"9:00 PM",
"10:00 PM",
"11:00 PM"
],
"2020-01-20T00:00:00.000Z",
[
"12:00 AM",
"1:00 AM",
"2:00 AM",
"3:00 AM",
"4:00 AM",
"5:00 AM",
"6:00 AM",
"7:00 AM",
"8:00 AM",
"9:00 AM",
"10:00 AM",
"11:00 AM",
"12:00 PM",
"1:00 PM",
"2:00 PM",
"3:00 PM",
"4:00 PM",
"5:00 PM",
"6:00 PM",
"7:00 PM",
"8:00 PM",
"9:00 PM",
"10:00 PM",
"11:00 PM"
],
"2020-01-21T00:00:00.000Z",
[
"12:00 AM",
"1:00 AM",
"2:00 AM",
"3:00 AM",
"4:00 AM",
"5:00 AM",
"6:00 AM",
"7:00 AM",
"8:00 AM",
"9:00 AM",
"10:00 AM",
"11:00 AM",
"12:00 PM",
"1:00 PM",
"2:00 PM",
"3:00 PM",
"4:00 PM",
"5:00 PM",
"6:00 PM",
"7:00 PM",
"8:00 PM",
"9:00 PM",
"10:00 PM",
"11:00 PM"
],
"2020-01-22T00:00:00.000Z",
[
"12:00 AM",
"1:00 AM",
"2:00 AM",
"3:00 AM",
"4:00 AM",
"5:00 AM",
"6:00 AM",
"7:00 AM",
"8:00 AM",
"9:00 AM",
"10:00 AM",
"11:00 AM",
"12:00 PM",
"1:00 PM",
"2:00 PM",
"3:00 PM",
"4:00 PM",
"5:00 PM",
"6:00 PM",
"7:00 PM",
"8:00 PM",
"9:00 PM",
"10:00 PM",
"11:00 PM"
],
"2020-01-23T00:00:00.000Z",
[
"12:00 AM",
"1:00 AM",
"2:00 AM",
"3:00 AM",
"4:00 AM",
"5:00 AM",
"6:00 AM",
"7:00 AM",
"8:00 AM",
"9:00 AM",
"10:00 AM",
"11:00 AM",
"12:00 PM",
"1:00 PM",
"2:00 PM",
"3:00 PM",
"4:00 PM",
"5:00 PM",
"6:00 PM",
"7:00 PM",
"8:00 PM",
"9:00 PM",
"10:00 PM",
"11:00 PM"
],
"2020-01-24T00:00:00.000Z",
[
"12:00 AM",
"1:00 AM",
"2:00 AM",
"3:00 AM",
"4:00 AM",
"5:00 AM",
"6:00 AM",
"7:00 AM",
"8:00 AM",
"9:00 AM",
"10:00 AM",
"11:00 AM",
"12:00 PM",
"1:00 PM",
"2:00 PM",
"3:00 PM",
"4:00 PM",
"5:00 PM",
"6:00 PM",
"7:00 PM",
"8:00 PM",
"9:00 PM",
"10:00 PM",
"11:00 PM"
],
"2020-01-25T00:00:00.000Z",
[
"12:00 AM",
"1:00 AM",
"2:00 AM",
"3:00 AM",
"4:00 AM",
"5:00 AM",
"6:00 AM",
"7:00 AM",
"8:00 AM",
"9:00 AM",
"10:00 AM",
"11:00 AM",
"12:00 PM",
"1:00 PM",
"2:00 PM",
"3:00 PM",
"4:00 PM",
"5:00 PM",
"6:00 PM",
"7:00 PM",
"8:00 PM",
"9:00 PM",
"10:00 PM",
"11:00 PM"
],
"2020-01-26T00:00:00.000Z",
[
"12:00 AM",
"1:00 AM",
"2:00 AM",
"3:00 AM",
"4:00 AM",
"5:00 AM",
"6:00 AM",
"7:00 AM",
"8:00 AM",
"9:00 AM",
"10:00 AM",
"11:00 AM",
"12:00 PM",
"1:00 PM",
"2:00 PM",
"3:00 PM",
"4:00 PM",
"5:00 PM",
"6:00 PM",
"7:00 PM",
"8:00 PM",
"9:00 PM",
"10:00 PM",
"11:00 PM"
],
"2020-01-27T00:00:00.000Z",
[
"12:00 AM",
"1:00 AM",
"2:00 AM",
"3:00 AM",
"4:00 AM",
"5:00 AM",
"6:00 AM",
"7:00 AM",
"8:00 AM",
"9:00 AM",
"10:00 AM",
"11:00 AM",
"12:00 PM",
"1:00 PM",
"2:00 PM",
"3:00 PM",
"4:00 PM",
"5:00 PM",
"6:00 PM",
"7:00 PM",
"8:00 PM",
"9:00 PM",
"10:00 PM",
"11:00 PM"
],
"2020-01-28T00:00:00.000Z",
[
"12:00 AM",
"1:00 AM",
"2:00 AM",
"3:00 AM",
"4:00 AM",
"5:00 AM",
"6:00 AM",
"7:00 AM",
"8:00 AM",
"9:00 AM",
"10:00 AM",
"11:00 AM",
"12:00 PM",
"1:00 PM",
"2:00 PM",
"3:00 PM",
"4:00 PM",
"5:00 PM",
"6:00 PM",
"7:00 PM",
"8:00 PM",
"9:00 PM",
"10:00 PM",
"11:00 PM"
],
"2020-01-29T00:00:00.000Z",
[
"12:00 AM",
"1:00 AM",
"2:00 AM",
"3:00 AM",
"4:00 AM",
"5:00 AM",
"6:00 AM",
"7:00 AM",
"8:00 AM",
"9:00 AM",
"10:00 AM",
"11:00 AM",
"12:00 PM",
"1:00 PM",
"2:00 PM",
"3:00 PM",
"4:00 PM",
"5:00 PM",
"6:00 PM",
"7:00 PM",
"8:00 PM",
"9:00 PM",
"10:00 PM",
"11:00 PM"
],
"2020-01-30T00:00:00.000Z",
[
"12:00 AM",
"1:00 AM",
"2:00 AM",
"3:00 AM",
"4:00 AM",
"5:00 AM",
"6:00 AM",
"7:00 AM",
"8:00 AM",
"9:00 AM",
"10:00 AM",
"11:00 AM",
"12:00 PM",
"1:00 PM",
"2:00 PM",
"3:00 PM",
"4:00 PM",
"5:00 PM",
"6:00 PM",
"7:00 PM",
"8:00 PM",
"9:00 PM",
"10:00 PM",
"11:00 PM"
],
"2020-01-31T00:00:00.000Z",
[
"12:00 AM",
"1:00 AM",
"2:00 AM",
"3:00 AM",
"4:00 AM",
"5:00 AM",
"6:00 AM",
"7:00 AM",
"8:00 AM",
"9:00 AM",
"10:00 AM",
"11:00 AM",
"12:00 PM",
"1:00 PM",
"2:00 PM",
"3:00 PM",
"4:00 PM",
"5:00 PM",
"6:00 PM",
"7:00 PM",
"8:00 PM",
"9:00 PM",
"10:00 PM",
"11:00 PM"
],
"2020-02-01T00:00:00.000Z",
[
"12:00 AM",
"1:00 AM",
"2:00 AM",
"3:00 AM",
"4:00 AM",
"5:00 AM",
"6:00 AM",
"7:00 AM",
"8:00 AM",
"9:00 AM",
"10:00 AM",
"11:00 AM",
"12:00 PM",
"1:00 PM",
"2:00 PM",
"3:00 PM",
"4:00 PM",
"5:00 PM",
"6:00 PM",
"7:00 PM",
"8:00 PM",
"9:00 PM",
"10:00 PM",
"11:00 PM"
],
"2020-02-02T00:00:00.000Z",
[
"12:00 AM",
"1:00 AM",
"2:00 AM",
"3:00 AM",
"4:00 AM",
"5:00 AM",
"6:00 AM",
"7:00 AM",
"8:00 AM",
"9:00 AM",
"10:00 AM",
"11:00 AM",
"12:00 PM",
"1:00 PM",
"2:00 PM",
"3:00 PM",
"4:00 PM",
"5:00 PM",
"6:00 PM",
"7:00 PM",
"8:00 PM",
"9:00 PM",
"10:00 PM",
"11:00 PM"
],
"2020-02-03T00:00:00.000Z",
[
"12:00 AM",
"1:00 AM",
"2:00 AM",
"3:00 AM",
"4:00 AM",
"5:00 AM",
"6:00 AM",
"7:00 AM",
"8:00 AM",
"9:00 AM",
"10:00 AM",
"11:00 AM",
"12:00 PM",
"1:00 PM",
"2:00 PM",
"3:00 PM",
"4:00 PM",
"5:00 PM",
"6:00 PM",
"7:00 PM",
"8:00 PM",
"9:00 PM",
"10:00 PM",
"11:00 PM"
],
"2020-02-04T00:00:00.000Z",
[
"12:00 AM",
"1:00 AM",
"2:00 AM",
"3:00 AM",
"4:00 AM",
"5:00 AM",
"6:00 AM",
"7:00 AM",
"8:00 AM",
"9:00 AM",
"10:00 AM",
"11:00 AM",
"12:00 PM",
"1:00 PM",
"2:00 PM",
"3:00 PM",
"4:00 PM",
"5:00 PM",
"6:00 PM",
"7:00 PM",
"8:00 PM",
"9:00 PM",
"10:00 PM",
"11:00 PM"
],
"2020-02-05T00:00:00.000Z",
[
"12:00 AM",
"1:00 AM",
"2:00 AM",
"3:00 AM",
"4:00 AM",
"5:00 AM",
"6:00 AM",
"7:00 AM",
"8:00 AM",
"9:00 AM",
"10:00 AM",
"11:00 AM",
"12:00 PM",
"1:00 PM",
"2:00 PM",
"3:00 PM",
"4:00 PM",
"5:00 PM",
"6:00 PM",
"7:00 PM",
"8:00 PM",
"9:00 PM",
"10:00 PM",
"11:00 PM"
],
"2020-02-06T00:00:00.000Z",
[
"12:00 AM",
"1:00 AM",
"2:00 AM",
"3:00 AM",
"4:00 AM",
"5:00 AM",
"6:00 AM",
"7:00 AM",
"8:00 AM",
"9:00 AM",
"10:00 AM",
"11:00 AM",
"12:00 PM",
"1:00 PM",
"2:00 PM",
"3:00 PM",
"4:00 PM",
"5:00 PM",
"6:00 PM",
"7:00 PM",
"8:00 PM",
"9:00 PM",
"10:00 PM",
"11:00 PM"
],
"2020-02-07T00:00:00.000Z",
[
"12:00 AM",
"1:00 AM",
"2:00 AM",
"3:00 AM",
"4:00 AM",
"5:00 AM",
"6:00 AM",
"7:00 AM",
"8:00 AM",
"9:00 AM",
"10:00 AM",
"11:00 AM",
"12:00 PM",
"1:00 PM",
"2:00 PM",
"3:00 PM",
"4:00 PM",
"5:00 PM",
"6:00 PM",
"7:00 PM",
"8:00 PM",
"9:00 PM",
"10:00 PM",
"11:00 PM"
],
"2020-02-08T00:00:00.000Z",
[
"12:00 AM",
"1:00 AM",
"2:00 AM",
"3:00 AM",
"4:00 AM",
"5:00 AM",
"6:00 AM",
"7:00 AM",
"8:00 AM",
"9:00 AM",
"10:00 AM",
"11:00 AM",
"12:00 PM",
"1:00 PM",
"2:00 PM",
"3:00 PM",
"4:00 PM",
"5:00 PM",
"6:00 PM",
"7:00 PM",
"8:00 PM",
"9:00 PM",
"10:00 PM",
"11:00 PM"
]
]
so instead of having this hour, I want to have the list of milliseconds of all the days in a milliseconds format

I try with this code, I'm not sure if this is what did you mean.
const date = function dateRange(startDate, endDate, steps = 1) {
const dateArray = [];
const currentDate = moment(startDate);
while (currentDate.isSameOrBefore(moment(endDate))) {
dateArray.push(moment(currentDate).format());
const hoursInMs = [];
for (let hour = 0; hour < 24; hour++) hoursInMs.push(moment(currentDate).add(hour, 'hours').format('x'));
dateArray.push(hoursInMs);
currentDate.add(steps, 'day');
}
return dateArray;
};
const startDateSchedule = '2020-01-08T00:00:00.000Z';
const endDateSchedule = '2020-01-12T00:00:00.000Z';
const dates = date(startDateSchedule, endDateSchedule);
console.log(dates);
// "2020-01-08T00:00:00.000Z",
// [ '1578441600000',
// '1578445200000',
// '1578448800000',
// '1578452400000',
// '1578456000000',
// '1578459600000',
// '1578463200000',
// '1578466800000',
// '1578470400000',
// '1578474000000',
// '1578477600000',
// '1578481200000',
// '1578484800000',
// '1578488400000',
// '1578492000000',
// '1578495600000',
// '1578499200000',
// '1578502800000',
// '1578506400000',
// '1578510000000',
// '1578513600000',
// '1578517200000',
// '1578520800000',
// '1578524400000' ],
// "2020-01-09T00:00:00.000Z",
// [ '1578528000000',
// '1578531600000',
// '1578535200000',
// '1578538800000',
// '1578542400000',
// '1578546000000',
// '1578549600000',
// '1578553200000',
// '1578556800000',
// '1578560400000',
// '1578564000000',
// '1578567600000',
// '1578571200000',
// '1578574800000',
// '1578578400000',
// '1578582000000',
// '1578585600000',
// '1578589200000',
// '1578592800000',
// '1578596400000',
// '1578600000000',
// '1578603600000',
// '1578607200000',
// '1578610800000' ],
// ... ]
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>

Related

Find whether elements in an array are consecutive with respective to another array [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I want to know if elements of array A are consecutive with respect to array B. For example
["9:00 AM", "9:30 AM", "10:00 AM"]
is consecutive with respect to
["9:00 AM", "9:30 AM", "10:00 AM", "10:30 AM", "11:00 AM", "11:30 AM", "12:00 PM", "12:30 PM", "1:00 PM", "1:30 PM", "2:00 PM", "2:30 PM", "3:00 PM", "3:30 PM", "4:00 PM", "4:30 PM"]
and
["9:00 AM", "10:30 AM", "11:00 AM"]
is not.
And if elements of array A are consecutive then return the index. For example
let all_appointments = ["9:00 AM", "9:30 AM", "10:00 AM", "10:30 AM", "11:00 AM", "11:30 AM", "12:00 PM", "12:30 PM", "1:00 PM", "1:30 PM", "2:00 PM", "2:30 PM", "3:00 PM", "3:30 PM", "4:00 PM", "4:30 PM"];
console.log(all_appointments.indexOf(["9:00 AM", "9:30 AM", "10:00 AM"]))
should print 0
function foobar(a, b) {
const g = "__t(T.Tt)__";
return b.join(g).indexOf(a.join(g)) === 0 ? 0 : undefined;
}
EDIT: added sad kirby to reduce chance of false positives
If the "reference array" cannot contain duplicates, this should suit your needs:
const indexOfArray = (needle, haystack) => {
if (!needle.length) {
return 0; // debatable
}
if (!haystack.length) {
return -1; // debatable
}
const haystackOffset = haystack.indexOf(needle[0]);
if (haystackOffset < 0) {
return -1;
}
for (let needleIndex = 1; needleIndex < needle.length; needleIndex++) {
const haystackIndex = needleIndex + haystackOffset;
if (haystack.length <= haystackIndex) {
return -1;
}
if (needle[needleIndex] !== haystack[haystackIndex]) {
return -1;
}
}
return haystackOffset;
};
Example:
const haystack = ["9:00 AM", "9:30 AM", "10:00 AM", "10:30 AM", "11:00 AM", "11:30 AM", "12:00 PM", "12:30 PM", "1:00 PM", "1:30 PM", "2:00 PM", "2:30 PM", "3:00 PM", "3:30 PM", "4:00 PM", "4:30 PM"];
indexOfArray(["9:00 AM", "9:30 AM"], haystack); // 0
indexOfArray(["9:30 AM", "10:00 AM", "10:30 AM"], haystack); // 1
indexOfArray(["10:30 AM"], haystack); // 3
indexOfArray(["9:00 AM", "10:00 AM"], haystack); // -1
indexOfArray(["4:30 PM", undefined], haystack); // -1

Running multiple sorts on JS array

I have this JS Array ...
var arr = [{
"Event_code": "BW-087",
"Interest_area": "Information technology",
"Start_time": "9:00 AM",
"End_time": "3:00 PM",
"Session_type": "Experience",
"all_day_evt": true
}, {
"Event_code": "BW-161",
"Interest_area": "Media, Communication and creative arts",
"Start_time": "9:00 AM",
"End_time": "3:00 PM",
"Session_type": "Experience",
"all_day_evt": true
}, {
"Event_code": "BW-114",
"Interest_area": "Nursing and midwifery",
"Start_time": "9:00 AM",
"End_time": "3:00 PM",
"Session_type": "Tour",
"all_day_evt": true
}, {
"Event_code": "BW-033",
"Interest_area": "",
"Start_time": "9:00 AM",
"End_time": "3:00 PM",
"Session_type": "General information session",
"all_day_evt": true
}, {
"Event_code": "BW-115",
"Interest_area": "Food, Nutrition and dietetics",
"Start_time": "9:30 AM",
"End_time": "3:00 PM",
"Session_type": "Tour",
"all_day_evt": true
}, {
"Event_code": "BW-060",
"Interest_area": "Sport",
"Start_time": "9:30 AM",
"End_time": "3:00 PM",
"Session_type": "Tour",
"all_day_evt": true
}, {
"Event_code": "BW-081",
"Interest_area": "Information technology",
"Start_time": "9:00 AM",
"End_time": "9:30 AM",
"Session_type": "Course information session",
"all_day_evt": false
}, {
"Event_code": "BW-170",
"Interest_area": "",
"Start_time": "9:30 AM",
"End_time": "10:30 AM",
"Session_type": "General information session",
"all_day_evt": false,
"clash": "This clashes with another session"
}, {
"Event_code": "BW-032",
"Interest_area": "",
"Start_time": "9:30 AM",
"End_time": "10:00 AM",
"Session_type": "General information session",
"all_day_evt": false
}, {
"Event_code": "BW-096",
"Interest_area": "Media, Communication and creative arts",
"Start_time": "10:00 AM",
"End_time": "12:00 PM",
"Session_type": "Experience",
"all_day_evt": false
}, {
"Event_code": "BW-163",
"Interest_area": "Business",
"Start_time": "10:00 AM",
"End_time": "2:00 PM",
"Session_type": "Experience",
"all_day_evt": false,
"clash": "This clashes with another session"
}, {
"Event_code": "BW-048",
"Interest_area": "Media, Communication and creative arts",
"Start_time": "12:00 PM",
"End_time": "12:30 PM",
"Session_type": "Experience",
"all_day_evt": false
}, {
"Event_code": "BW-128",
"Interest_area": "Media, Communication and creative arts",
"Start_time": "12:00 PM",
"End_time": "12:30 PM",
"Session_type": "Tour",
"all_day_evt": false,
"clash": "This clashes with another session"
}, {
"Event_code": "BW-018",
"Interest_area": "Sciences",
"Start_time": "12:30 PM",
"End_time": "2:30 PM",
"Session_type": "Tour",
"all_day_evt": false,
"clash": "This clashes with another session"
}, {
"Event_code": "BW-013",
"Interest_area": "Sciences, Engineering, Information technology, Architecture and built environment, Environment",
"Start_time": "12:30 PM",
"End_time": "1:00 PM",
"Session_type": "Course information session",
"all_day_evt": false
}, {
"Event_code": "BW-039",
"Interest_area": "Media, Communication and creative arts",
"Start_time": "1:00 PM",
"End_time": "1:30 PM",
"Session_type": "Experience",
"all_day_evt": false
}, {
"Event_code": "BW-162",
"Interest_area": "Education and teaching",
"Start_time": "1:00 PM",
"End_time": "1:30 PM",
"Session_type": "Tour",
"all_day_evt": false,
"clash": "This clashes with another session"
}]
arr.sort((x,y) => y.all_day_evt- x.all_day_evt || ((new Date('1970/01/01 ' + x.Start_time)) - (new Date('1970/01/01 ' + y.Start_time))));
console.log(arr);
I am looking to do 3 operations on this array ...
Group this array by "all_day_evt" key.
Sort this array within the 'true' subgroup, and 'false' subgroup by 'Start_time'.
Sort if there is a "clash" on 'start_time' ... with the "clashed event" showing up 2nd.
I am not sure how to implement the 3rd condition, any help is very appreciated. Thanks
Sample output shown below - highlighting how the clash events should line up. It should show up so that the event with "clash" key comes 2nd, not first.
For example, events "BW-013" & "BW-018" are clashing at start_time of 12:30 PM. So, 'BW-013' comes first, and then 'BW-018' shows up. Same goes with events 'BW-039' and 'BW-162' as shown in example output below.
[{
"Event_code": "BW-013",
"Interest_area": "Sciences, Engineering, Information technology, Architecture and built environment, Environment",
"Start_time": "12:30 PM",
"End_time": "1:00 PM",
"Session_type": "Course information session",
"all_day_evt": false
},
{
"Event_code": "BW-018",
"Interest_area": "Sciences",
"Start_time": "12:30 PM",
"End_time": "2:30 PM",
"Session_type": "Tour",
"all_day_evt": false,
"clash": "This clashes with another session"
},
{
"Event_code": "BW-039",
"Interest_area": "Media, Communication and creative arts",
"Start_time": "1:00 PM",
"End_time": "1:30 PM",
"Session_type": "Experience",
"all_day_evt": false
}, {
"Event_code": "BW-162",
"Interest_area": "Education and teaching",
"Start_time": "1:00 PM",
"End_time": "1:30 PM",
"Session_type": "Tour",
"all_day_evt": false,
"clash": "This clashes with another session"
}
]
Try following. You need to add additional condition of clash
var arr = [{"Event_code":"BW-087","Interest_area":"Information technology","Start_time":"9:00 AM","End_time":"3:00 PM","Session_type":"Experience","all_day_evt":true},{"Event_code":"BW-161","Interest_area":"Media, Communication and creative arts","Start_time":"9:00 AM","End_time":"3:00 PM","Session_type":"Experience","all_day_evt":true},{"Event_code":"BW-114","Interest_area":"Nursing and midwifery","Start_time":"9:00 AM","End_time":"3:00 PM","Session_type":"Tour","all_day_evt":true},{"Event_code":"BW-033","Interest_area":"","Start_time":"9:00 AM","End_time":"3:00 PM","Session_type":"General information session","all_day_evt":true},{"Event_code":"BW-115","Interest_area":"Food, Nutrition and dietetics","Start_time":"9:30 AM","End_time":"3:00 PM","Session_type":"Tour","all_day_evt":true},{"Event_code":"BW-060","Interest_area":"Sport","Start_time":"9:30 AM","End_time":"3:00 PM","Session_type":"Tour","all_day_evt":true},{"Event_code":"BW-081","Interest_area":"Information technology","Start_time":"9:00 AM","End_time":"9:30 AM","Session_type":"Course information session","all_day_evt":false},{"Event_code":"BW-170","Interest_area":"","Start_time":"9:30 AM","End_time":"10:30 AM","Session_type":"General information session","all_day_evt":false,"clash":"This clashes with another session"},{"Event_code":"BW-032","Interest_area":"","Start_time":"9:30 AM","End_time":"10:00 AM","Session_type":"General information session","all_day_evt":false},{"Event_code":"BW-096","Interest_area":"Media, Communication and creative arts","Start_time":"10:00 AM","End_time":"12:00 PM","Session_type":"Experience","all_day_evt":false},{"Event_code":"BW-163","Interest_area":"Business","Start_time":"10:00 AM","End_time":"2:00 PM","Session_type":"Experience","all_day_evt":false,"clash":"This clashes with another session"},{"Event_code":"BW-048","Interest_area":"Media, Communication and creative arts","Start_time":"12:00 PM","End_time":"12:30 PM","Session_type":"Experience","all_day_evt":false},{"Event_code":"BW-128","Interest_area":"Media, Communication and creative arts","Start_time":"12:00 PM","End_time":"12:30 PM","Session_type":"Tour","all_day_evt":false,"clash":"This clashes with another session"},{"Event_code":"BW-018","Interest_area":"Sciences","Start_time":"12:30 PM","End_time":"2:30 PM","Session_type":"Tour","all_day_evt":false,"clash":"This clashes with another session"},{"Event_code":"BW-013","Interest_area":"Sciences, Engineering, Information technology, Architecture and built environment, Environment","Start_time":"12:30 PM","End_time":"1:00 PM","Session_type":"Course information session","all_day_evt":false},{"Event_code":"BW-039","Interest_area":"Media, Communication and creative arts","Start_time":"1:00 PM","End_time":"1:30 PM","Session_type":"Experience","all_day_evt":false},{"Event_code":"BW-162","Interest_area":"Education and teaching","Start_time":"1:00 PM","End_time":"1:30 PM","Session_type":"Tour","all_day_evt":false,"clash":"This clashes with another session"}];
arr.sort((x,y) => y.all_day_evt- x.all_day_evt || ((new Date('1970/01/01 ' + x.Start_time)) - (new Date('1970/01/01 ' + y.Start_time))) || Boolean(x.clash) - Boolean(y.clash));
console.log(arr);

Using jQuery date picker to dynamically populate dropdown list

I have a form that a user fills out. First the user selects a store. Then the user selects a date from a calendar, using the jQuery Datepicker plugin. Based on which store the user selects and which DAY of the week the user selects from the calendar (Monday, Tuesday, etc). then that will determine which times will appear in the third dropdown. Here is my html:
<form action="/CustomerService-FFEventBookAppointment">
<fieldset>
<div class="form-row"><label for="store">Preferred Store *</label>
<div class="chzn-row valid">
<select class="chzn-global-select input-select optional required" id="store" name="store">
<option value=""></option>
<option value="Martin Place">Martin Place</option>
<option value="Chatswood Chase">Chatswood Chase</option>
<option value="Emporium">Emporium</option>
<option value="Indooroopilly">Indooroopilly</option>
<option value="Collins Street">Collins Street</option>
<option value="DJ Market Street">DJ Market Street</option>
<option value="DJ Bourke Street Mens">DJ Bourke Street Mens</option>
<option value="DJ Bourke Street Womens">DJ Bourke Street Womens</option>
<option value="DJ Queen Street">DJ Queen Street</option>
<option value="DJ Rundle Mall">DJ Rundle Mall</option>
</select>
</div>
</div>
<div class="form-row"><label for="date">Date *</label>
And below is my JavaScript. You can view the full thing on my JS fiddle: https://jsfiddle.net/katherinekonn/qquprwhg/
<div class="chzn-row valid">
<input type="text" lass="chzn-global-select input-select optional required" id="datepicker" name="date">
</div>
</div>
<div class="form-row"><label for="time">Time * </label>
<div class="chzn-row valid">
<select class="chzn-global-select input-select optional required" id="time" name="time">
<option value=""></option>
</select>
</div>
</div>
var dayByStore = {
"Martin Place": ["Friday", "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday"],
"Chatswood Chase": ["Friday", "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday"],
};
var timeByDayByStore = {
"Martin Place":{
"Friday":[
"9:00 AM - 10:00 AM",
"6:00 PM - 7:00 PM"
],
"Saturday":[
"10:00 AM - 11:00 AM",
"11:00 AM - 12:00 PM",
"4:00 PM - 5:00 PM"
],
"Sunday":[
"11:00 AM - 12:00 PM",
"12:00 PM - 1:00 PM",
"2:00 PM - 3:00 PM",
"3:00 PM - 4:00 PM",
"4:00 PM - 5:00 PM"
],
"Monday":[
"9:00 AM - 10:00 AM",
"6:00 PM - 7:00 PM"
],
"Tuesday":[
"9:00 AM - 10:00 AM"
],
"Wednesday":[
"9:00 AM - 10:00 AM",
"6:00 PM - 7:00 PM"
],
"Thursday":[
"9:00 AM - 10:00 AM",
"7:00 PM - 8:00 PM"
],
},
"Chatswood Chase":{
"Friday":[
"10:00 AM - 11:00 AM",
"4:00 PM - 5:00 PM"
],
"Saturday":[
"9:00 AM - 10:00 AM",
"4:00 PM - 5:00 PM"
],
"Sunday":[
"10:00 AM - 11:00 AM",
"4:00 PM - 5:00 PM"
],
"Monday":[
"10:00 AM - 11:00 AM",
"4:00 PM - 5:00 PM"
],
"Tuesday":[
"10:00 AM - 11:00 AM",
"4:00 PM - 5:00 PM"
],
"Wednesday":[
"10:00 AM - 11:00 AM",
"11:00 AM - 12:00 PM",
"4:00 PM - 5:00 PM"
],
"Thursday":[
"8:00 PM - 9:00 PM"
],
},
};
$( function() {
$('#datepicker').datepicker({ dateFormat: 'DD, MM, d, yy' });
} );
$(document).ready(function(){
$(document).on('change', '#datepicker', function(){
var store = $('#store option:selected').val();
var timeOptions = "";
for (timeId in timeByDayByStore[store][date]) {
timeOptions += "<option value='" + timeByDayByStore[store][date][timeId] + "'>" + timeByDayByStore[store][date][timeId] + "</option>";
}
document.getElementById("time").innerHTML = timeOptions;
})
})
Can anyone help me with the function? I'm still very new, but I need to somehow have the time dropdown become populated by times based on what store and what day of the week from the calendar the user selects, and I can't quite figure out the logic. Help is greatly appreciated and thank you in advance!
Here is a working snippet,
where I only made modifications at the end of the JS.
I removed the "personal" parts to make the snippet shorter,
and added some comments in my code to make it clearer!
var dayByStore = {
"Martin Place": ["Friday", "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday"],
"Chatswood Chase": ["Friday", "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday"],
"Emporium": ["Friday", "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday"],
"Indooroopilly": ["Friday", "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday"],
"Collins Street": ["Friday", "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday"],
"DJ Market Street": ["Friday", "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday"],
"DJ Bourke Street Mens": ["Friday", "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday"],
"DJ Bourke Street Womens": ["Friday", "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday"],
"DJ Queen Street": ["Friday", "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday"],
"DJ Rundle Mall": ["Friday", "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday"],
};
var timeByDayByStore = {
"Martin Place": {
"Friday": [
"9:00 AM - 10:00 AM",
"10:00 AM - 11:00 AM",
"11:00 AM - 12:00 PM",
"12:00 PM - 1:00 PM",
"1:00 PM - 2:00 PM",
"2:00 PM - 3:00 PM",
"3:00 PM - 4:00 PM",
"4:00 PM - 5:00 PM",
"5:00 PM - 6:00 PM",
"6:00 PM - 7:00 PM"
],
"Saturday": [
"10:00 AM - 11:00 AM",
"11:00 AM - 12:00 PM",
"12:00 PM - 1:00 PM",
"1:00 PM - 2:00 PM",
"2:00 PM - 3:00 PM",
"3:00 PM - 4:00 PM",
"4:00 PM - 5:00 PM"
],
"Sunday": [
"11:00 AM - 12:00 PM",
"12:00 PM - 1:00 PM",
"1:00 PM - 2:00 PM",
"2:00 PM - 3:00 PM",
"3:00 PM - 4:00 PM",
"4:00 PM - 5:00 PM"
],
"Monday": [
"9:00 AM - 10:00 AM",
"10:00 AM - 11:00 AM",
"11:00 AM - 12:00 PM",
"12:00 PM - 1:00 PM",
"1:00 PM - 2:00 PM",
"2:00 PM - 3:00 PM",
"3:00 PM - 4:00 PM",
"4:00 PM - 5:00 PM",
"5:00 PM - 6:00 PM",
"6:00 PM - 7:00 PM"
],
"Tuesday": [
"9:00 AM - 10:00 AM",
"10:00 AM - 11:00 AM",
"11:00 AM - 12:00 PM",
"12:00 PM - 1:00 PM",
"1:00 PM - 2:00 PM",
"2:00 PM - 3:00 PM",
"3:00 PM - 4:00 PM",
"4:00 PM - 5:00 PM",
"5:00 PM - 6:00 PM",
"6:00 PM - 7:00 PM"
],
"Wednesday": [
"9:00 AM - 10:00 AM",
"10:00 AM - 11:00 AM",
"11:00 AM - 12:00 PM",
"12:00 PM - 1:00 PM",
"1:00 PM - 2:00 PM",
"2:00 PM - 3:00 PM",
"3:00 PM - 4:00 PM",
"4:00 PM - 5:00 PM",
"5:00 PM - 6:00 PM",
"6:00 PM - 7:00 PM"
],
"Thursday": [
"9:00 AM - 10:00 AM",
"10:00 AM - 11:00 AM",
"11:00 AM - 12:00 PM",
"12:00 PM - 1:00 PM",
"1:00 PM - 2:00 PM",
"2:00 PM - 3:00 PM",
"3:00 PM - 4:00 PM",
"4:00 PM - 5:00 PM",
"5:00 PM - 6:00 PM",
"6:00 PM - 7:00 PM",
"7:00 PM - 8:00 PM"
],
},
"Chatswood Chase": {
"Friday": [
"10:00 AM - 11:00 AM",
"11:00 AM - 12:00 PM",
"12:00 PM - 1:00 PM",
"1:00 PM - 2:00 PM",
"2:00 PM - 3:00 PM",
"3:00 PM - 4:00 PM",
"4:00 PM - 5:00 PM"
],
"Saturday": [
"9:00 AM - 10:00 AM",
"10:00 AM - 11:00 AM",
"11:00 AM - 12:00 PM",
"12:00 PM - 1:00 PM",
"1:00 PM - 2:00 PM",
"2:00 PM - 3:00 PM",
"3:00 PM - 4:00 PM",
"4:00 PM - 5:00 PM"
],
"Sunday": [
"10:00 AM - 11:00 AM",
"11:00 AM - 12:00 PM",
"12:00 PM - 1:00 PM",
"1:00 PM - 2:00 PM",
"2:00 PM - 3:00 PM",
"3:00 PM - 4:00 PM",
"4:00 PM - 5:00 PM"
],
"Monday": [
"10:00 AM - 11:00 AM",
"11:00 AM - 12:00 PM",
"12:00 PM - 1:00 PM",
"1:00 PM - 2:00 PM",
"2:00 PM - 3:00 PM",
"3:00 PM - 4:00 PM",
"4:00 PM - 5:00 PM"
],
"Tuesday": [
"10:00 AM - 11:00 AM",
"11:00 AM - 12:00 PM",
"12:00 PM - 1:00 PM",
"1:00 PM - 2:00 PM",
"2:00 PM - 3:00 PM",
"3:00 PM - 4:00 PM",
"4:00 PM - 5:00 PM"
],
"Wednesday": [
"10:00 AM - 11:00 AM",
"11:00 AM - 12:00 PM",
"12:00 PM - 1:00 PM",
"1:00 PM - 2:00 PM",
"2:00 PM - 3:00 PM",
"3:00 PM - 4:00 PM",
"4:00 PM - 5:00 PM"
],
"Thursday": [
"10:00 AM - 11:00 AM",
"11:00 AM - 12:00 PM",
"12:00 PM - 1:00 PM",
"1:00 PM - 2:00 PM",
"2:00 PM - 3:00 PM",
"3:00 PM - 4:00 PM",
"4:00 PM - 5:00 PM",
"5:00 PM - 6:00 PM",
"6:00 PM - 7:00 PM",
"7:00 PM - 8:00 PM",
"8:00 PM - 9:00 PM"
],
},
"Emporium": {
"Friday": [
"10:00 AM - 11:00 AM",
"11:00 AM - 12:00 PM",
"12:00 PM - 1:00 PM",
"1:00 PM - 2:00 PM",
"2:00 PM - 3:00 PM",
"3:00 PM - 4:00 PM",
"4:00 PM - 5:00 PM",
"5:00 PM - 6:00 PM",
"6:00 PM - 7:00 PM",
"7:00 PM - 8:00 PM",
"8:00 PM - 9:00 PM"
],
"Saturday": [
"10:00 AM - 11:00 AM",
"11:00 AM - 12:00 PM",
"12:00 PM - 1:00 PM",
"1:00 PM - 2:00 PM",
"2:00 PM - 3:00 PM",
"3:00 PM - 4:00 PM",
"4:00 PM - 5:00 PM",
"5:00 PM - 6:00 PM",
"6:00 PM - 7:00 PM"
],
"Sunday": [
"10:00 AM - 11:00 AM",
"11:00 AM - 12:00 PM",
"12:00 PM - 1:00 PM",
"1:00 PM - 2:00 PM",
"2:00 PM - 3:00 PM",
"3:00 PM - 4:00 PM",
"4:00 PM - 5:00 PM",
"5:00 PM - 6:00 PM",
"6:00 PM - 7:00 PM"
],
"Monday": [
"10:00 AM - 11:00 AM",
"11:00 AM - 12:00 PM",
"12:00 PM - 1:00 PM",
"1:00 PM - 2:00 PM",
"2:00 PM - 3:00 PM",
"3:00 PM - 4:00 PM",
"4:00 PM - 5:00 PM",
"5:00 PM - 6:00 PM",
"6:00 PM - 7:00 PM"
],
"Tuesday": [
"10:00 AM - 11:00 AM",
"11:00 AM - 12:00 PM",
"12:00 PM - 1:00 PM",
"1:00 PM - 2:00 PM",
"2:00 PM - 3:00 PM",
"3:00 PM - 4:00 PM",
"4:00 PM - 5:00 PM",
"5:00 PM - 6:00 PM",
"6:00 PM - 7:00 PM"
],
"Wesnesday": [
"10:00 AM - 11:00 AM",
"11:00 AM - 12:00 PM",
"12:00 PM - 1:00 PM",
"1:00 PM - 2:00 PM",
"2:00 PM - 3:00 PM",
"3:00 PM - 4:00 PM",
"4:00 PM - 5:00 PM",
"5:00 PM - 6:00 PM",
"6:00 PM - 7:00 PM"
],
"Thursday": [
"10:00 AM - 11:00 AM",
"11:00 AM - 12:00 PM",
"12:00 PM - 1:00 PM",
"1:00 PM - 2:00 PM",
"2:00 PM - 3:00 PM",
"3:00 PM - 4:00 PM",
"4:00 PM - 5:00 PM",
"5:00 PM - 6:00 PM",
"6:00 PM - 7:00 PM",
"7:00 PM - 8:00 PM",
"8:00 PM - 9:00 PM"
],
},
"Indooroopilly": {
"Friday": [
"9:00 AM - 10:00 AM",
"10:00 AM - 11:00 AM",
"11:00 AM - 12:00 PM",
"12:00 PM - 1:00 PM",
"1:00 PM - 2:00 PM",
"2:00 PM - 3:00 PM",
"3:00 PM - 4:00 PM",
"4:00 PM - 5:00 PM"
],
"Saturday": [
"9:00 AM - 10:00 AM",
"10:00 AM - 11:00 AM",
"11:00 AM - 12:00 PM",
"12:00 PM - 1:00 PM",
"1:00 PM - 2:00 PM",
"2:00 PM - 3:00 PM",
"3:00 PM - 4:00 PM",
"4:00 PM - 5:00 PM"
],
"Sunday": [
"10:00 AM - 11:00 AM",
"11:00 AM - 12:00 PM",
"12:00 PM - 1:00 PM",
"1:00 PM - 2:00 PM",
"2:00 PM - 3:00 PM",
"3:00 PM - 4:00 PM"
],
"Monday": [
"9:00 AM - 10:00 AM",
"10:00 AM - 11:00 AM",
"11:00 AM - 12:00 PM",
"12:00 PM - 1:00 PM",
"1:00 PM - 2:00 PM",
"2:00 PM - 3:00 PM",
"3:00 PM - 4:00 PM",
"4:00 PM - 5:00 PM"
],
"Tuesday": [
"9:00 AM - 10:00 AM",
"10:00 AM - 11:00 AM",
"11:00 AM - 12:00 PM",
"12:00 PM - 1:00 PM",
"1:00 PM - 2:00 PM",
"2:00 PM - 3:00 PM",
"3:00 PM - 4:00 PM",
"4:00 PM - 5:00 PM"
],
"Wednesday": [
"9:00 AM - 10:00 AM",
"10:00 AM - 11:00 AM",
"11:00 AM - 12:00 PM",
"12:00 PM - 1:00 PM",
"1:00 PM - 2:00 PM",
"2:00 PM - 3:00 PM",
"3:00 PM - 4:00 PM",
"4:00 PM - 5:00 PM"
],
"Thursday": [
"9:00 AM - 10:00 AM",
"10:00 AM - 11:00 AM",
"11:00 AM - 12:00 PM",
"12:00 PM - 1:00 PM",
"1:00 PM - 2:00 PM",
"2:00 PM - 3:00 PM",
"3:00 PM - 4:00 PM",
"4:00 PM - 5:00 PM",
"5:00 PM - 6:00 PM",
"6:00 PM - 7:00 PM",
"7:00 PM - 8:00 PM",
"8:00 PM - 9:00 PM"
],
},
"Collins Street": {
"Friday": [
"9:00 AM - 10:00 AM",
"10:00 AM - 11:00 AM",
"11:00 AM - 12:00 PM",
"12:00 PM - 1:00 PM",
"1:00 PM - 2:00 PM",
"2:00 PM - 3:00 PM",
"3:00 PM - 4:00 PM",
"4:00 PM - 5:00 PM",
"5:00 PM - 6:00 PM",
"6:00 PM - 7:00 PM"
],
"Saturday": [
"10:00 AM - 11:00 AM",
"11:00 AM - 12:00 PM",
"12:00 PM - 1:00 PM",
"1:00 PM - 2:00 PM",
"2:00 PM - 3:00 PM",
"3:00 PM - 4:00 PM",
"4:00 PM - 5:00 PM"
],
"Monday": [
"9:00 AM - 10:00 AM",
"10:00 AM - 11:00 AM",
"11:00 AM - 12:00 PM",
"12:00 PM - 1:00 PM",
"1:00 PM - 2:00 PM",
"2:00 PM - 3:00 PM",
"3:00 PM - 4:00 PM",
"4:00 PM - 5:00 PM",
"5:00 PM - 6:00 PM"
],
"Tuesday": [
"9:00 AM - 10:00 AM",
"10:00 AM - 11:00 AM",
"11:00 AM - 12:00 PM",
"12:00 PM - 1:00 PM",
"1:00 PM - 2:00 PM",
"2:00 PM - 3:00 PM",
"3:00 PM - 4:00 PM",
"4:00 PM - 5:00 PM",
"5:00 PM - 6:00 PM"
],
"Wednesday": [
"9:00 AM - 10:00 AM",
"10:00 AM - 11:00 AM",
"11:00 AM - 12:00 PM",
"12:00 PM - 1:00 PM",
"1:00 PM - 2:00 PM",
"2:00 PM - 3:00 PM",
"3:00 PM - 4:00 PM",
"4:00 PM - 5:00 PM",
"5:00 PM - 6:00 PM"
],
"Thursday": [
"9:00 AM - 10:00 AM",
"10:00 AM - 11:00 AM",
"11:00 AM - 12:00 PM",
"12:00 PM - 1:00 PM",
"1:00 PM - 2:00 PM",
"2:00 PM - 3:00 PM",
"3:00 PM - 4:00 PM",
"4:00 PM - 5:00 PM",
"5:00 PM - 6:00 PM"
],
},
"DJ Market Street": {
"Friday": [
"10:00 AM - 11:00 AM",
"11:00 AM - 12:00 PM",
"12:00 PM - 1:00 PM",
"1:00 PM - 2:00 PM",
"2:00 PM - 3:00 PM",
"3:00 PM - 4:00 PM",
"4:00 PM - 5:00 PM",
"5:00 PM - 6:00 PM",
"6:00 PM - 7:00 PM",
"7:00 PM - 8:00 PM",
"8:00 PM - 9:00 PM"
],
"Saturday": [
"9:00 AM - 10:00 AM",
"10:00 AM - 11:00 AM",
"11:00 AM - 12:00 PM",
"12:00 PM - 1:00 PM",
"1:00 PM - 2:00 PM",
"2:00 PM - 3:00 PM",
"3:00 PM - 4:00 PM",
"4:00 PM - 5:00 PM",
"5:00 PM - 6:00 PM",
"6:00 PM - 7:00 PM"
],
"Sunday": [
"10:00 AM - 11:00 AM",
"11:00 AM - 12:00 PM",
"12:00 PM - 1:00 PM",
"1:00 PM - 2:00 PM",
"2:00 PM - 3:00 PM",
"3:00 PM - 4:00 PM",
"4:00 PM - 5:00 PM",
"5:00 PM - 6:00 PM",
"6:00 PM - 7:00 PM"
],
"Monday": [
"10:00 AM - 11:00 AM",
"11:00 AM - 12:00 PM",
"12:00 PM - 1:00 PM",
"1:00 PM - 2:00 PM",
"2:00 PM - 3:00 PM",
"3:00 PM - 4:00 PM",
"4:00 PM - 5:00 PM",
"5:00 PM - 6:00 PM",
"6:00 PM - 7:00 PM"
],
"Tuesday": [
"10:00 AM - 11:00 AM",
"11:00 AM - 12:00 PM",
"12:00 PM - 1:00 PM",
"1:00 PM - 2:00 PM",
"2:00 PM - 3:00 PM",
"3:00 PM - 4:00 PM",
"4:00 PM - 5:00 PM",
"5:00 PM - 6:00 PM",
"6:00 PM - 7:00 PM"
],
"Wednesday": [
"10:00 AM - 11:00 AM",
"11:00 AM - 12:00 PM",
"12:00 PM - 1:00 PM",
"1:00 PM - 2:00 PM",
"2:00 PM - 3:00 PM",
"3:00 PM - 4:00 PM",
"4:00 PM - 5:00 PM",
"5:00 PM - 6:00 PM",
"6:00 PM - 7:00 PM"
],
"Thursday": [
"10:00 AM - 11:00 AM",
"11:00 AM - 12:00 PM",
"12:00 PM - 1:00 PM",
"1:00 PM - 2:00 PM",
"2:00 PM - 3:00 PM",
"3:00 PM - 4:00 PM",
"4:00 PM - 5:00 PM",
"5:00 PM - 6:00 PM",
"6:00 PM - 7:00 PM",
"7:00 PM - 8:00 PM",
"8:00 PM - 9:00 PM"
],
},
"DJ Bourke Street Mens": {
"Friday": [
"10:00 AM - 11:00 AM",
"11:00 AM - 12:00 PM",
"12:00 PM - 1:00 PM",
"1:00 PM - 2:00 PM",
"2:00 PM - 3:00 PM",
"3:00 PM - 4:00 PM",
"4:00 PM - 5:00 PM",
"5:00 PM - 6:00 PM",
"6:00 PM - 7:00 PM",
"7:00 PM - 8:00 PM",
"8:00 PM - 9:00 PM"
],
"Saturday": [
"10:00 AM - 11:00 AM",
"11:00 AM - 12:00 PM",
"12:00 PM - 1:00 PM",
"1:00 PM - 2:00 PM",
"2:00 PM - 3:00 PM",
"3:00 PM - 4:00 PM",
"4:00 PM - 5:00 PM",
"5:00 PM - 6:00 PM",
"6:00 PM - 7:00 PM"
],
"Sunday": [
"10:00 AM - 11:00 AM",
"11:00 AM - 12:00 PM",
"12:00 PM - 1:00 PM",
"1:00 PM - 2:00 PM",
"2:00 PM - 3:00 PM",
"3:00 PM - 4:00 PM",
"4:00 PM - 5:00 PM",
"5:00 PM - 6:00 PM",
"6:00 PM - 7:00 PM"
],
"Monday": [
"10:00 AM - 11:00 AM",
"11:00 AM - 12:00 PM",
"12:00 PM - 1:00 PM",
"1:00 PM - 2:00 PM",
"2:00 PM - 3:00 PM",
"3:00 PM - 4:00 PM",
"4:00 PM - 5:00 PM",
"5:00 PM - 6:00 PM",
"6:00 PM - 7:00 PM"
],
"Tuesday": [
"10:00 AM - 11:00 AM",
"11:00 AM - 12:00 PM",
"12:00 PM - 1:00 PM",
"1:00 PM - 2:00 PM",
"2:00 PM - 3:00 PM",
"3:00 PM - 4:00 PM",
"4:00 PM - 5:00 PM",
"5:00 PM - 6:00 PM",
"6:00 PM - 7:00 PM"
],
"Wednesday": [
"10:00 AM - 11:00 AM",
"11:00 AM - 12:00 PM",
"12:00 PM - 1:00 PM",
"1:00 PM - 2:00 PM",
"2:00 PM - 3:00 PM",
"3:00 PM - 4:00 PM",
"4:00 PM - 5:00 PM",
"5:00 PM - 6:00 PM",
"6:00 PM - 7:00 PM"
],
"Thursday": [
"10:00 AM - 11:00 AM",
"11:00 AM - 12:00 PM",
"12:00 PM - 1:00 PM",
"1:00 PM - 2:00 PM",
"2:00 PM - 3:00 PM",
"3:00 PM - 4:00 PM",
"4:00 PM - 5:00 PM",
"5:00 PM - 6:00 PM",
"6:00 PM - 7:00 PM",
"7:00 PM - 8:00 PM",
"8:00 PM - 9:00 PM"
],
},
"DJ Bourke Street Womens": {
"Friday": [
"10:00 AM - 11:00 AM",
"11:00 AM - 12:00 PM",
"12:00 PM - 1:00 PM",
"1:00 PM - 2:00 PM",
"2:00 PM - 3:00 PM",
"3:00 PM - 4:00 PM",
"4:00 PM - 5:00 PM",
"5:00 PM - 6:00 PM",
"6:00 PM - 7:00 PM",
"7:00 PM - 8:00 PM",
"8:00 PM - 9:00 PM"
],
"Saturday": [
"10:00 AM - 11:00 AM",
"11:00 AM - 12:00 PM",
"12:00 PM - 1:00 PM",
"1:00 PM - 2:00 PM",
"2:00 PM - 3:00 PM",
"3:00 PM - 4:00 PM",
"4:00 PM - 5:00 PM",
"5:00 PM - 6:00 PM",
"6:00 PM - 7:00 PM"
],
"Sunday": [
"10:00 AM - 11:00 AM",
"11:00 AM - 12:00 PM",
"12:00 PM - 1:00 PM",
"1:00 PM - 2:00 PM",
"2:00 PM - 3:00 PM",
"3:00 PM - 4:00 PM",
"4:00 PM - 5:00 PM",
"5:00 PM - 6:00 PM",
"6:00 PM - 7:00 PM"
],
"Monday": [
"10:00 AM - 11:00 AM",
"11:00 AM - 12:00 PM",
"12:00 PM - 1:00 PM",
"1:00 PM - 2:00 PM",
"2:00 PM - 3:00 PM",
"3:00 PM - 4:00 PM",
"4:00 PM - 5:00 PM",
"5:00 PM - 6:00 PM",
"6:00 PM - 7:00 PM"
],
"Tuesday": [
"10:00 AM - 11:00 AM",
"11:00 AM - 12:00 PM",
"12:00 PM - 1:00 PM",
"1:00 PM - 2:00 PM",
"2:00 PM - 3:00 PM",
"3:00 PM - 4:00 PM",
"4:00 PM - 5:00 PM",
"5:00 PM - 6:00 PM",
"6:00 PM - 7:00 PM"
],
"Wednesday": [
"10:00 AM - 11:00 AM",
"11:00 AM - 12:00 PM",
"12:00 PM - 1:00 PM",
"1:00 PM - 2:00 PM",
"2:00 PM - 3:00 PM",
"3:00 PM - 4:00 PM",
"4:00 PM - 5:00 PM",
"5:00 PM - 6:00 PM",
"6:00 PM - 7:00 PM"
],
"Thursday": [
"10:00 AM - 11:00 AM",
"11:00 AM - 12:00 PM",
"12:00 PM - 1:00 PM",
"1:00 PM - 2:00 PM",
"2:00 PM - 3:00 PM",
"3:00 PM - 4:00 PM",
"4:00 PM - 5:00 PM",
"5:00 PM - 6:00 PM",
"6:00 PM - 7:00 PM",
"7:00 PM - 8:00 PM",
"8:00 PM - 9:00 PM"
],
},
"DJ Queen Street": {
"Friday": [
"10:00 AM - 11:00 AM",
"11:00 AM - 12:00 PM",
"12:00 PM - 1:00 PM",
"1:00 PM - 2:00 PM",
"2:00 PM - 3:00 PM",
"3:00 PM - 4:00 PM",
"4:00 PM - 5:00 PM",
"5:00 PM - 6:00 PM",
"6:00 PM - 7:00 PM",
"7:00 PM - 8:00 PM",
"8:00 PM - 9:00 PM"
],
"Saturday": [
"9:00 AM - 10:00 AM",
"10:00 AM - 11:00 AM",
"11:00 AM - 12:00 PM",
"12:00 PM - 1:00 PM",
"1:00 PM - 2:00 PM",
"2:00 PM - 3:00 PM",
"3:00 PM - 4:00 PM",
"4:00 PM - 5:00 PM"
],
"Sunday": [
"10:00 AM - 11:00 AM",
"11:00 AM - 12:00 PM",
"12:00 PM - 1:00 PM",
"1:00 PM - 2:00 PM",
"2:00 PM - 3:00 PM",
"3:00 PM - 4:00 PM",
"4:00 PM - 5:00 PM",
"5:00 PM - 6:00 PM"
],
"Monday": [
"10:00 AM - 11:00 AM",
"11:00 AM - 12:00 PM",
"12:00 PM - 1:00 PM",
"1:00 PM - 2:00 PM",
"2:00 PM - 3:00 PM",
"3:00 PM - 4:00 PM",
"4:00 PM - 5:00 PM",
"5:00 PM - 6:00 PM",
"6:00 PM - 7:00 PM"
],
"Tuesday": [
"10:00 AM - 11:00 AM",
"11:00 AM - 12:00 PM",
"12:00 PM - 1:00 PM",
"1:00 PM - 2:00 PM",
"2:00 PM - 3:00 PM",
"3:00 PM - 4:00 PM",
"4:00 PM - 5:00 PM",
"5:00 PM - 6:00 PM",
"6:00 PM - 7:00 PM"
],
"Wednesday": [
"10:00 AM - 11:00 AM",
"11:00 AM - 12:00 PM",
"12:00 PM - 1:00 PM",
"1:00 PM - 2:00 PM",
"2:00 PM - 3:00 PM",
"3:00 PM - 4:00 PM",
"4:00 PM - 5:00 PM",
"5:00 PM - 6:00 PM",
"6:00 PM - 7:00 PM"
],
"Thursday": [
"10:00 AM - 11:00 AM",
"11:00 AM - 12:00 PM",
"12:00 PM - 1:00 PM",
"1:00 PM - 2:00 PM",
"2:00 PM - 3:00 PM",
"3:00 PM - 4:00 PM",
"4:00 PM - 5:00 PM",
"5:00 PM - 6:00 PM",
"6:00 PM - 7:00 PM",
"7:00 PM - 8:00 PM",
"8:00 PM - 9:00 PM"
],
},
"DJ Rundle Mall": {
"Friday": [
"10:00 AM - 11:00 AM",
"11:00 AM - 12:00 PM",
"12:00 PM - 1:00 PM",
"1:00 PM - 2:00 PM",
"2:00 PM - 3:00 PM",
"3:00 PM - 4:00 PM",
"4:00 PM - 5:00 PM",
"5:00 PM - 6:00 PM",
"6:00 PM - 7:00 PM",
"7:00 PM - 8:00 PM",
"8:00 PM - 9:00 PM"
],
"Saturday": [
"9:00 AM - 10:00 AM",
"10:00 AM - 11:00 AM",
"11:00 AM - 12:00 PM",
"12:00 PM - 1:00 PM",
"1:00 PM - 2:00 PM",
"2:00 PM - 3:00 PM",
"3:00 PM - 4:00 PM",
"4:00 PM - 5:00 PM"
],
"Sunday": [
"11:00 AM - 12:00 PM",
"12:00 PM - 1:00 PM",
"1:00 PM - 2:00 PM",
"2:00 PM - 3:00 PM",
"3:00 PM - 4:00 PM",
"4:00 PM - 5:00 PM"
],
"Monday": [
"10:00 AM - 11:00 AM",
"11:00 AM - 12:00 PM",
"12:00 PM - 1:00 PM",
"1:00 PM - 2:00 PM",
"2:00 PM - 3:00 PM",
"3:00 PM - 4:00 PM",
"4:00 PM - 5:00 PM",
"5:00 PM - 6:00 PM",
"6:00 PM - 7:00 PM"
],
"Tuesday": [
"10:00 AM - 11:00 AM",
"11:00 AM - 12:00 PM",
"12:00 PM - 1:00 PM",
"1:00 PM - 2:00 PM",
"2:00 PM - 3:00 PM",
"3:00 PM - 4:00 PM",
"4:00 PM - 5:00 PM",
"5:00 PM - 6:00 PM",
"6:00 PM - 7:00 PM"
],
"Wednesday": [
"10:00 AM - 11:00 AM",
"11:00 AM - 12:00 PM",
"12:00 PM - 1:00 PM",
"1:00 PM - 2:00 PM",
"2:00 PM - 3:00 PM",
"3:00 PM - 4:00 PM",
"4:00 PM - 5:00 PM",
"5:00 PM - 6:00 PM",
"6:00 PM - 7:00 PM"
],
"Thursday": [
"10:00 AM - 11:00 AM",
"11:00 AM - 12:00 PM",
"12:00 PM - 1:00 PM",
"1:00 PM - 2:00 PM",
"2:00 PM - 3:00 PM",
"3:00 PM - 4:00 PM",
"4:00 PM - 5:00 PM",
"5:00 PM - 6:00 PM",
"6:00 PM - 7:00 PM",
"7:00 PM - 8:00 PM",
"8:00 PM - 9:00 PM"
],
},
};
var user_store = '';
var user_day = '';
// Function where the options are updated
function update_time() {
// Empty the options list
$('#time').empty();
// Exits function if there's no store, or no day.
if (!user_store || !user_day) return;
// Gets the open hours of the user selected store and day
var open_hours = timeByDayByStore[user_store][user_day];
// Fills the options list
$.each(open_hours, function(key, value) {
$('#time')
.append($("<option></option>")
.attr("value", key)
.text(value));
});
}
$(function() {
$('#datepicker').datepicker({
dateFormat: 'DD, MM, d, yy',
// Added action on selection of a date
onSelect: function(dateText, inst) {
// Here, we split the value of the datepicker to get the day, like "Monday"
user_day = dateText.split(',')[0];
// Then, we call the update function!
update_time();
}
});
});
// Call the function when changing the store, too!
$('#store').on('change', function(){
user_store = $('#store').val();
update_time();
});
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>jQuery UI Datepicker - Default functionality</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
</head>
<body>
<div class="ffEventContainer">
<div id="ffEvent">
<div id="ffHeader">
<div class="line"> </div>
<div class="line lineRight"> </div>
<h1>Time & Venue</h1>
</div>
<div id="ffLanding">
<form action="/CustomerService-FFEventBookAppointment">
<fieldset>
<div class="form-row"><label for="store">Preferred Store *</label>
<div class="chzn-row valid">
<select class="chzn-global-select input-select optional required" id="store" name="store">
<option value=""></option>
<option value="Martin Place">Martin Place</option>
<option value="Chatswood Chase">Chatswood Chase</option>
<option value="Emporium">Emporium</option>
<option value="Indooroopilly">Indooroopilly</option>
<option value="Collins Street">Collins Street</option>
<option value="DJ Market Street">DJ Market Street</option>
<option value="DJ Bourke Street Mens">DJ Bourke Street Mens</option>
<option value="DJ Bourke Street Womens">DJ Bourke Street Womens</option>
<option value="DJ Queen Street">DJ Queen Street</option>
<option value="DJ Rundle Mall">DJ Rundle Mall</option>
</select>
</div>
</div>
<div class="form-row"><label for="date">Date *</label>
<div class="chzn-row valid">
<input type="text" lass="chzn-global-select input-select optional required" id="datepicker" name="date">
</div>
</div>
<div class="form-row"><label for="time">Time * </label>
<div class="chzn-row valid">
<select class="chzn-global-select input-select optional required" id="time" name="time">
<option value=""></option>
</select>
</div>
</div>
</fieldset>
</form>
</div>
</div>
</div>
</body>
</html>
I hope it helps.
You may take a look on my JS fiddle: https://jsfiddle.net/1bx2ccb0/1/
const time = document.getElementById("time");
$(document).ready(function() {
$(document).on('change', '#datepicker', function() {
var store = $('#store option:selected').val();
const date = document.getElementById("datepicker").value.split(',')[0];
time.innerHTML = "";
timeByDayByStore[store][date].forEach(timeByDay => {
const option = new Option(timeByDay, timeByDay)
time.add(option);
});
})
})

Conditional filtering in JSON

I have this JSON file ...
[{
"Event_code": "AB-001",
"Interest_area": "Arts and Education",
"Start_time": "9:00 AM",
"End_time": "3:00 PM",
"Session_type": "Course information session"
}, {
"Event_code": "AB-002",
"Interest_area": "Arts and Education",
"Start_time": "12:30 PM",
"End_time": "1:00 PM",
"Session_type": "Course information session"
}, {
"Event_code": "AB-003",
"Interest_area": "",
"Start_time": "9:00 AM",
"End_time": "3:00 PM",
"Session_type": "Course information session"
}, {
"Event_code": "AB-004",
"Interest_area": "Business",
"Start_time": "10:30 AM",
"End_time": "11:00 AM",
"Session_type": "Course information session"
}, {
"Event_code": "AB-005",
"Interest_area": "General Interest",
"Start_time": "9:30 AM",
"End_time": "1:30 PM",
"Session_type": "Experience"
}, {
"Event_code": "AB-006",
"Interest_area": "Environment , Business ",
"Start_time": "11:00 AM",
"End_time": "11:30 AM",
"Session_type": "Course information session"
}]
I want to filter this JSON where "Session_type" is equal to "Course information session" and "Interest_area" is equal to "Business"
What I have tried so far ....
let search = 'Course information session';
let result = [...new Set(arr.filter(o=>o.Session_type === search && o.Interest_area.trim() !== '' && o.Interest_area === "Business"))]
console.log(result);
This code only returns event with Event_code 'AB-004' and NOT 'AB-006' I want to return both AB-004 and AB-006
Can someone please advise what am I doing wrong ? Thanks
Use String.includes
let arr = [{ "Event_code": "AB-001", "Interest_area": "Arts and Education", "Start_time": "9:00 AM", "End_time": "3:00 PM", "Session_type": "Course information session" }, { "Event_code": "AB-002", "Interest_area": "Arts and Education", "Start_time": "12:30 PM","End_time": "1:00 PM", "Session_type": "Course information session" }, { "Event_code": "AB-003", "Interest_area": "", "Start_time": "9:00 AM", "End_time": "3:00 PM", "Session_type": "Course information session" }, { "Event_code": "AB-004", "Interest_area":"Business", "Start_time": "10:30 AM", "End_time": "11:00 AM", "Session_type": "Course information session" }, { "Event_code": "AB-005", "Interest_area": "General Interest", "Start_time": "9:30 AM", "End_time": "1:30 PM", "Session_type": "Experience" },{ "Event_code": "AB-006", "Interest_area": "Environment , Business ", "Start_time": "11:00 AM", "End_time": "11:30 AM", "Session_type": "Course information session" }];
let search = 'Course information session', interestArea = "Business";
let result = arr.filter(o=> o.Session_type === search && o.Interest_area.includes(interestArea));
console.log(result);
Note: Use Set only if you want to remove duplicates, however, there are no duplicates in the array, hence, you can avoid using it.
Use string#includes instead of === equality comparison, since you want to determines whether one string may be found within another string.
let arr = [{ "Event_code": "AB-001", "Interest_area": "Arts and Education", "Start_time": "9:00 AM", "End_time": "3:00 PM", "Session_type": "Course information session" }, { "Event_code": "AB-002", "Interest_area": "Arts and Education", "Start_time": "12:30 PM","End_time": "1:00 PM", "Session_type": "Course information session" }, { "Event_code": "AB-003", "Interest_area": "", "Start_time": "9:00 AM", "End_time": "3:00 PM", "Session_type": "Course information session" }, { "Event_code": "AB-004", "Interest_area":"Business", "Start_time": "10:30 AM", "End_time": "11:00 AM", "Session_type": "Course information session" }, { "Event_code": "AB-005", "Interest_area": "General Interest", "Start_time": "9:30 AM", "End_time": "1:30 PM", "Session_type": "Experience" },{ "Event_code": "AB-006", "Interest_area": "Environment , Business ", "Start_time": "11:00 AM", "End_time": "11:30 AM", "Session_type": "Course information session" }],
search = 'Course information session',
result = arr.filter(o => o.Session_type === search && o.Interest_area.trim() !== '' && o.Interest_area.includes("Business"));
console.log(result);
Take a look at Interest_area at AB-006 :
"Interest_area": "Environment , Business ",
Interest_area === "Business" will always be false. Try this :
Interest_area.includes("Business")
Will return true.

Parse JSON string and get key value

I have following JSON
[{ "GameID":"1109100", "StartTime": "4:00 PM", "EndTime": "1/1/1900 5:30:00 PM", "Duration": "90","TeamID":"101567"},{ "GameID":"1108768", "StartTime": "12:00 PM", "EndTime": "1/1/1900 1:30:00 PM", "Duration": "90","TeamID":"109873"},{ "GameID":"1108776", "StartTime": "12:00 PM", "EndTime": "1/1/1900 1:30:00 PM", "Duration": "90","TeamID":"117674"},{ "GameID":"1108742", "StartTime": "9:00 AM", "EndTime": "1/1/1900 10:30:00 AM", "Duration": "90","TeamID":"24621"},{ "GameID":"1108751", "StartTime": "9:00 AM", "EndTime": "1/1/1900 10:30:00 AM", "Duration": "90","TeamID":"24622"},{ "GameID":"1108757", "StartTime": "10:30 AM", "EndTime": "1/1/1900 12:00:00 PM", "Duration": "90","TeamID":"24623"},{ "GameID":"1109098", "StartTime": "2:30 PM", "EndTime": "1/1/1900 4:00:00 PM", "Duration": "90","TeamID":"24746"}]
I want to get game id, start time and end time value using TeamID. How can I do this ?
Edit: Actual function to parse JSON from comments
function doParseTime(msg){
var data = $.parseJSON(msg);
$.each(data, function() {
$.each(this, function(key, val){
$("#"+key).val(val);
});
});
}
Here a simple code-snippet how to access your json-keys:
$(document).ready(function(){
msg = '[{ "GameID":"1109100", "StartTime": "4:00 PM", "EndTime": "1/1/1900 5:30:00 PM", "Duration": "90","TeamID":"101567"},{ "GameID":"1108768", "StartTime": "12:00 PM", "EndTime": "1/1/1900 1:30:00 PM", "Duration": "90","TeamID":"109873"},{ "GameID":"1108776", "StartTime": "12:00 PM", "EndTime": "1/1/1900 1:30:00 PM", "Duration": "90","TeamID":"117674"},{ "GameID":"1108742", "StartTime": "9:00 AM", "EndTime": "1/1/1900 10:30:00 AM", "Duration": "90","TeamID":"24621"},{ "GameID":"1108751", "StartTime": "9:00 AM", "EndTime": "1/1/1900 10:30:00 AM", "Duration": "90","TeamID":"24622"},{ "GameID":"1108757", "StartTime": "10:30 AM", "EndTime": "1/1/1900 12:00:00 PM", "Duration": "90","TeamID":"24623"},{ "GameID":"1109098", "StartTime": "2:30 PM", "EndTime": "1/1/1900 4:00:00 PM", "Duration": "90","TeamID":"24746"}] ';
$.each(JSON.parse(msg), function(idx, obj) {
var gameid = obj.GameID;
var starttime = obj.StartTime;
var endtime = obj.EndTime;
});
});
Next time please read more in google about parsing and accessing json with jquery or javascript ;)
var yourJsonStr='[{ "GameID":"1109100", "StartTime": "4:00 PM", "EndTime": "1/1/1900 5:30:00 PM", "Duration": "90","TeamID":"101567"},{ "GameID":"1108768", "StartTime": "12:00 PM", "EndTime": "1/1/1900 1:30:00 PM", "Duration": "90","TeamID":"109873"},{ "GameID":"1108776", "StartTime": "12:00 PM", "EndTime": "1/1/1900 1:30:00 PM", "Duration": "90","TeamID":"117674"},{ "GameID":"1108742", "StartTime": "9:00 AM", "EndTime": "1/1/1900 10:30:00 AM", "Duration": "90","TeamID":"24621"},{ "GameID":"1108751", "StartTime": "9:00 AM", "EndTime": "1/1/1900 10:30:00 AM", "Duration": "90","TeamID":"24622"},{ "GameID":"1108757", "StartTime": "10:30 AM", "EndTime": "1/1/1900 12:00:00 PM", "Duration": "90","TeamID":"24623"},{ "GameID":"1109098", "StartTime": "2:30 PM", "EndTime": "1/1/1900 4:00:00 PM", "Duration": "90","TeamID":"24746"}]';
var gameInfo=eval("("+yourJsonStr+")");
$.each(gameInfo,function(i,item){
$('#gameId').append((i + 1) + ": " + item.GameID + "<br/>");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<div id="gameId"></div>
Try with JSON.parse() ,Array#filter and Array#map
var data = '[{ "GameID":"1109100", "StartTime": "4:00 PM", "EndTime": "1/1/1900 5:30:00 PM", "Duration": "90","TeamID":"101567"},{ "GameID":"1108768", "StartTime": "12:00 PM", "EndTime": "1/1/1900 1:30:00 PM", "Duration": "90","TeamID":"109873"},{ "GameID":"1108776", "StartTime": "12:00 PM", "EndTime": "1/1/1900 1:30:00 PM", "Duration": "90","TeamID":"117674"},{ "GameID":"1108742", "StartTime": "9:00 AM", "EndTime": "1/1/1900 10:30:00 AM", "Duration": "90","TeamID":"24621"},{ "GameID":"1108751", "StartTime": "9:00 AM", "EndTime": "1/1/1900 10:30:00 AM", "Duration": "90","TeamID":"24622"},{ "GameID":"1108757", "StartTime": "10:30 AM", "EndTime": "1/1/1900 12:00:00 PM", "Duration": "90","TeamID":"24623"},{ "GameID":"1109098", "StartTime": "2:30 PM", "EndTime": "1/1/1900 4:00:00 PM", "Duration": "90","TeamID":"24746"}]';
function finding(id){
var f = JSON.parse(data).filter(a=> a.TeamID == id) //it will the matched teamId
return f.map(a=> {return {
GameID : a.GameID,
StartTime: a.StartTime,
EndTime : a.EndTime,
}}) // it add compress with three key value and iterate the all matched object's
}
console.log(finding(109873))
Parse the JSON if it's a string, then iterate over the array of objects by using
Array.find().
From the docs:
The find() method returns the value of the first element in the array
that satisfies the provided testing function. Otherwise undefined is
returned.
let json = JSON.parse(yourJsonString);
let teamID= yourTeamID;
//json is an array of javascript objects. Iterate over them with "find";
//This will return the first instance where the expression evaluates to true
let foundObject = json.find(obj=>{
return obj.TeamID=== teamID;
});
let gameID = foundObject.GameID;
let startTime = foundObject.StartTime;
let endTime = foundObject.EndTime;
Hope this helps.

Categories