admin管理员组文章数量:1026989
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 bined 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 binedOutput
As it stands, the notes are in their correct date order per note type. However, when it es time to bined 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 = [],
binedOutput = [],
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(binedOutput[noteDate]) == 'undefined') {
binedOutput[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
binedOutput[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 binedOutput
by its key, which happens to be a date object.
This is a screenshot of how the binedOutput
array looks right now with no sorting.
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 bined 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 binedOutput
As it stands, the notes are in their correct date order per note type. However, when it es time to bined 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 = [],
binedOutput = [],
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(binedOutput[noteDate]) == 'undefined') {
binedOutput[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
binedOutput[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 binedOutput
by its key, which happens to be a date object.
This is a screenshot of how the binedOutput
array looks right now with no sorting.
- 2 Objects in javascript are never guaranteed to be sorted. If order is important, you should use an array – Brennan Commented May 2, 2017 at 20:11
2 Answers
Reset to default 5You can use the Array.sort() method. Passing a function you can specify how to pare values. Here the link to the sort method:
https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort?v=example
So, in your example, get the data from the server inside an array. Then you can order them in this way: You can have something like this with your output:
resultArray.sort(function(a, b) {
var dateA = new Date(a.noteDate);
var dateB = new Date(b.noteDate);
if (dateA < dateB ) {
return -1;
}
if (dateA > dateB ) {
return 1;
}
return 0;
});
Then after you have your data sorted, repeat your procedure for iterating over them and display values. You should already have them ordered.
Here is a Greate Answer.
Basically use sort() and inside it convert your dates into Date Objects and do a parison. The link will show you how this is done. It would be a quicker sort if you added a date object to the stored object in the array so you would not have to construct and discard so many objects at sort time.
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 bined 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 binedOutput
As it stands, the notes are in their correct date order per note type. However, when it es time to bined 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 = [],
binedOutput = [],
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(binedOutput[noteDate]) == 'undefined') {
binedOutput[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
binedOutput[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 binedOutput
by its key, which happens to be a date object.
This is a screenshot of how the binedOutput
array looks right now with no sorting.
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 bined 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 binedOutput
As it stands, the notes are in their correct date order per note type. However, when it es time to bined 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 = [],
binedOutput = [],
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(binedOutput[noteDate]) == 'undefined') {
binedOutput[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
binedOutput[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 binedOutput
by its key, which happens to be a date object.
This is a screenshot of how the binedOutput
array looks right now with no sorting.
- 2 Objects in javascript are never guaranteed to be sorted. If order is important, you should use an array – Brennan Commented May 2, 2017 at 20:11
2 Answers
Reset to default 5You can use the Array.sort() method. Passing a function you can specify how to pare values. Here the link to the sort method:
https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort?v=example
So, in your example, get the data from the server inside an array. Then you can order them in this way: You can have something like this with your output:
resultArray.sort(function(a, b) {
var dateA = new Date(a.noteDate);
var dateB = new Date(b.noteDate);
if (dateA < dateB ) {
return -1;
}
if (dateA > dateB ) {
return 1;
}
return 0;
});
Then after you have your data sorted, repeat your procedure for iterating over them and display values. You should already have them ordered.
Here is a Greate Answer.
Basically use sort() and inside it convert your dates into Date Objects and do a parison. The link will show you how this is done. It would be a quicker sort if you added a date object to the stored object in the array so you would not have to construct and discard so many objects at sort time.
本文标签: Javascript sort array by date keyStack Overflow
版权声明:本文标题:Javascript sort array by date key - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745656384a2161621.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论