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