I have a javascript loop that is iterating over some notes in a database and concatenating them into a string to append to the DOM. It is using the noteType
as the key so I can separate the different types of notes on multiple tabs.
The issue is, I need to show a combined view of all notes sorted by date.So at the end of the loop, I append the notes to a separate variable that I call combinedOutput
As it stands, the notes are in their correct date order per note type. However, when it comes time to combined them all as one, two sets of correctly ordered arrays are being appended in their respective order and throwing off the date order in the final output.
Here is an example of my code:
// Define our vars
var output = [],
combinedOutput = [],
noteType = '',
noteDate = new Date;
$(data).find('notes>data').each(function() {
var $p = $(this);
noteType = $p.find('noteType').text(),
noteDate = moment.utc($p.find('timestampOrig').text()).toDate()
if (typeof(output[noteType]) == 'undefined') {
output[noteType] = "";
}
if (typeof(combinedOutput[noteDate]) == 'undefined') {
combinedOutput[noteDate] = new Date;
}
// Create our note
output[noteType] += '<div id="message_' + $p.find('recordID').text() + '" class="panel panel-default custom-panel item">';
output[noteType] += 'Something Here';
output[noteType] += '</div>';
// Append to our final output variable
combinedOutput[noteDate] += output[noteType];
});
Dates Example
**Note Type: Public**
April 1, 2017
March 5, 2017
April 8, 2017
**Note Type: Private**
April 2, 2017
March 9, 2017
March 11, 2017
**Combined Notes:**
April 1, 2017
March 5, 2017
April 8, 2017
April 2, 2017
March 9, 2017
March 11, 2017
My end goal here is to sort the combinedOutput
by its key, which happens to be a date object.
This is a screenshot of how the combinedOutput
array looks right now with no sorting.