I'm trying to filter out elements from an array until a specific string, which in this case are day names (Mon, Tue, Wed etc.) which I get with this variable:
var day = new Date(reservation.startDate);
I have an array of elements and I'm using the the push() method to add them inside the array.
Here is the array:
console.log(JSON.stringify(arrayData, null, 4))
[
"<b>Mon</b>",
"<b>AUT14</b>",
"<b>KL25AB55200-3001</b>",
"<b>Mon</b>",
"<b>AUT15</b>",
"<b>KC23DK20010-3003</b>",
"<b>Tue</b>",
"<b>TI14</b>",
"<b>KL04BT10110-3001</b>",
"<b>Tue</b>",
"<b>AUT15</b>",
"<b>KL25AB10451-3001</b>",
"<b>Tue</b>",
"<b>AUT13</b>",
"<b>AUT13</b>",
"<b>KL25AD10000-14</b>",
"<b>Tue</b>",
"<b>ASR14</b>",
"<b>Wed</b>",
"<b>TI14</b>",
"<b>IPS16</b>",
"<b>A800BD65-3001</b>",
"<b>Wed</b>",
"<b>TI14</b>",
"<b>KL04BT10110-3001</b>",
"<b>Wed</b>",
"<b>AUT15</b>",
"<b>KL25AB55200-3002</b>",
"<b>Thu</b>",
"<b>AUT16</b>",
"<b>KL25AB10250-3001</b>",
"<b>Thu</b>",
"<b>TI14</b>",
"<b>IPS16</b>",
"<b>A800BD65-3001</b>",
"<b>Thu</b>",
"<b>TI13</b>",
"<b>TI14</b>",
"<b>KL25AB10300-3001</b>"
]
If I want to print out the data that only contains "Mon", I use:
if (day.toString().indexOf("Mon") >= 0) {
document.getElementById("Monday").innerHTML = arrayData.join("");
}
But if I use the same method on wednesday for example, I get the data from monday, tuesday and up until wednesday, which is not ideal since I only want the data from wednesday.
if (day.toString().indexOf("Wed") >= 0) {
document.getElementById("Wednesday").innerHTML = arrayData.join("");
}
So is there an effective way to pick data from the middle of the array and not include other days?
I have to use core JavaScript, so any frameworks can't be used.
To clarify
I need to print the data from each day into their own innerHTML sections.
So I would need this data for ("Monday").innerHTML :
"<b>Mon</b>",
"<b>AUT14</b>",
"<b>KL25AB55200-3001</b>",
"<b>Mon</b>",
"<b>AUT15</b>",
"<b>KC23DK20010-3003</b>",
("Tuesday").innerHTML:
"<b>Tue</b>",
"<b>TI14</b>",
"<b>KL04BT10110-3001</b>",
"<b>Tue</b>",
"<b>AUT15</b>",
"<b>KL25AB10451-3001</b>",
"<b>Tue</b>",
"<b>AUT13</b>",
"<b>AUT13</b>",
"<b>KL25AD10000-14</b>",
"<b>Tue</b>",
"<b>ASR14</b>",
("Wednesday").innerHTML:
"<b>Wed</b>",
"<b>TI14</b>",
"<b>KL04BT10110-3001</b>",
"<b>Wed</b>",
"<b>AUT15</b>",
"<b>KL25AB55200-3002</b>",
("Thursday").innerHTML:
"<b>Thu</b>",
"<b>AUT16</b>",
"<b>KL25AB10250-3001</b>",
"<b>Thu</b>",
"<b>TI14</b>",
"<b>IPS16</b>",
"<b>A800BD65-3001</b>",
"<b>Thu</b>",
"<b>TI13</b>",
"<b>TI14</b>",
"<b>KL25AB10300-3001</b>"
Note that the data changes from day to day, so should I just filter out the other days and it's data from the array I don't want to include in the innerHTML?
arrayData? It's not clear from your example. Is each line that you printed out one element, and the commas are part of that element's text? Or are the elements separated by commas and the newlines aren't significant - in other words each line in your printout is three or sometimes four consecutive array elements? If it's the latter, this seems like an awfully awkward data format to work with. Do you have the option of changing this format to be easier to use?<br/>at the end, others don't. And in a couple of cases, the last element in a group is missing its</b>closing tag. Your first step has to be to clean up this data.console.log( JSON.stringify(arrayData,null,4) )to get an unambiguous representation of your data so people aren't confused about its actual format.