Running multiple sorts on JS array - javascript

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

Related

Javascript Date and time

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>

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

Finding two values existing in same object array using javascript

I have an array with flight detail like from, to and cost of the tickets. I'm trying to show the flight details for particular from-to.
For example: if the user selects from as bangaluru and to as delhi then I should show the corresponding detail.I used set to have the print the unique values.Is there any way to check if from and to belongs to same object then it should show details of the object.
I tried the below, but it just checks the overall array.
if((flightDetails.includes(orginVal)) && (flightDetails.includes(destinationVal))){
alert("hello");
}
Js code:
flightDetails=[{
"airline": "B-201",
"from": "Bangaluru(BLR)",
"to": "Delhi(DEL)",
"detail": [{
"date": "2019-12-30",
"price": "3900",
"departTime": "12:00 PM",
"arriveTime": "14:00 PM",
"seats":"10"
}, {
"date": "2019-12-31",
"price": "3000",
"departTime": "17:30 PM",
"arriveTime": "19:30 PM",
"seats":"3"
}, {
"date": "2019-06-01",
"price": "2100",
"departTime": "09:00 AM",
"arriveTime": "11:00 AM",
"seats":"7"
}]
},{
"airline": "B-202",
"from": "Delhi(DEL)",
"to": "Bangaluru(BLR)",
"detail": [{
"date": "2019-12-30",
"price": "3000",
"departTime": "12:00 PM",
"arriveTime": "14:00 PM",
"seats":"10"
}, {
"date": "2019-12-31",
"price": "3000",
"departTime": "17:30 PM",
"arriveTime": "19:30 PM",
"seats":"3"
}, {
"date": "2019-06-01",
"price": "2100",
"departTime": "09:00 AM",
"arriveTime": "11:00 AM",
"seats":"7"
}]
}]
inputOrigin=document.getElementById('origin');
inputDesination= document.getElementById("desination");
originOptions=document.getElementById('originCountry');
destinationOptions= document.getElementById('desinationCountry');
var originCategories = new Set();
var destinationCategories = new Set();
flightDetails.forEach((o) => originCategories.add(o.from));
originCategories = [...originCategories];
flightDetails.forEach((o) => destinationCategories.add(o.to));
destinationCategories = [...destinationCategories];
for(i=0;i<originCategories.length;i++) {
originOptions.innerHTML+=' <option>'+originCategories[i]+'<option>';
}
for(i=0;i<destinationCategories.length;i++) {
destinationOptions.innerHTML+=' <option>'+destinationCategories[i]+'<option>';
}
You can use filter, which will return an array with matches. If you only expect one, you can of course pop that element out of it:
var flightDetails=[{"airline": "B-201","from": "Bangaluru(BLR)","to": "Delhi(DEL)","detail": [{"date": "2019-12-30","price": "3900","departTime": "12:00 PM","arriveTime": "14:00 PM","seats":"10"}, {"date": "2019-12-31","price": "3000","departTime": "17:30 PM","arriveTime": "19:30 PM","seats":"3"}, {"date": "2019-06-01","price": "2100","departTime": "09:00 AM","arriveTime": "11:00 AM","seats":"7"}]},{"airline": "B-202","from": "Delhi(DEL)","to": "Bangaluru(BLR)","detail": [{"date": "2019-12-30","price": "3000","departTime": "12:00 PM","arriveTime": "14:00 PM","seats":"10"}, {"date": "2019-12-31","price": "3000","departTime": "17:30 PM","arriveTime": "19:30 PM","seats":"3"}, {"date": "2019-06-01","price": "2100","departTime": "09:00 AM","arriveTime": "11:00 AM","seats":"7"}]}];
var originVal = "Delhi(DEL)";
var destinationVal = "Bangaluru(BLR)"
var matches = flightDetails.filter(detail => detail.from === originVal && detail.to === destinationVal);
console.log(matches);
As your existing code already collects the exact origins and destinations, from which the user makes a selection, I don't think it is useful to use includes. It will be more appropriate to look for exact matches.
filter will possibly return multiple results, so you should probably give the user a further selection possibility from that result list.
You're comparing a js object rather than the from-to strings.
Use the function find and check for each object the attributes from-to. This approach finds the first match and not the remaining matches, in that case, you can use the function filter instead.
I'm assuming the final user will write a string like originVal="Bangal" and destinationVal="Del"
let flightDetails = [{ "airline": "B-201", "from": "Bangaluru(BLR)", "to": "Delhi(DEL)", "detail": [{ "date": "2019-12-30", "price": "3900", "departTime": "12:00 PM", "arriveTime": "14:00 PM", "seats": "10" }, { "date": "2019-12-31", "price": "3000", "departTime": "17:30 PM", "arriveTime": "19:30 PM", "seats": "3" }, { "date": "2019-06-01", "price": "2100", "departTime": "09:00 AM", "arriveTime": "11:00 AM", "seats": "7" }]}, { "airline": "B-202", "from": "Delhi(DEL)", "to": "Bangaluru(BLR)", "detail": [{ "date": "2019-12-30", "price": "3000", "departTime": "12:00 PM", "arriveTime": "14:00 PM", "seats": "10" }, { "date": "2019-12-31", "price": "3000", "departTime": "17:30 PM", "arriveTime": "19:30 PM", "seats": "3" }, { "date": "2019-06-01", "price": "2100", "departTime": "09:00 AM", "arriveTime": "11:00 AM", "seats": "7" }]}],
originVal = "Bangaluru",
destinationVal = "Delhi",
found = flightDetails.find(({from, to}) => from.includes(originVal) && to.includes(destinationVal));
if (found) console.log(found.detail);
.as-console-wrapper { max-height: 100% !important; top: 0; }
You can take input and than find the object based on from and to property
let flightDetails = [{"airline":"B-201","from":"Bangaluru(BLR)","to":"Delhi(DEL)","detail":[{"date":"2019-12-30","price":"3900","departTime":"12:00PM","arriveTime":"14:00PM","seats":"10"},{"date":"2019-12-31","price":"3000","departTime":"17:30PM","arriveTime":"19:30PM","seats":"3"},{"date":"2019-06-01","price":"2100","departTime":"09:00AM","arriveTime":"11:00AM","seats":"7"}]},{"airline":"B-202","from":"Delhi(DEL)","to":"Bangaluru(BLR)","detail":[{"date":"2019-12-30","price":"3000","departTime":"12:00PM","arriveTime":"14:00PM","seats":"10"},{"date":"2019-12-31","price":"3000","departTime":"17:30PM","arriveTime":"19:30PM","seats":"3"},{"date":"2019-06-01","price":"2100","departTime":"09:00AM","arriveTime":"11:00AM","seats":"7"}]}]
function values(){
let fromIn = document.getElementById('from').value
let toIn = document.getElementById('to').value
let details = flightDetails.find(( {from,to} ) => from === fromIn && to === toIn)
console.log(details)
}
<input id='from' placeholder='From'></input>
<input id='to' placeHolder='To'></input>
<button onClick=values()>Give me details</button>

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