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 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.