使用日期值按单个键对对象数组进行排序
在本文中,我们将讨论使用日期值按单键对对象数组进行排序的方法,并了解以下内容:
- 什么是数组
- 什么是对象
- 什么是对象数组
- JavaScript 中的
array.sort
原型是什么 - 如何使用日期值对对象数组进行排序
在 JavaScript 中,数组将多种数据类型存储在一个变量中。与其他一些编程语言不同,JavaScript 数组可以在同一个数组中保存不同的数据类型。
数组中的每一项都一个接一个地存储在 RAM 中。
const randomArray = ["Tahseen", 1, 0.3,true];
console.log(randomArray);
// logs: Tahseen , 1 , 0.3 , true
数组中的每一项都可以通过将其索引传递给数组变量来访问,
console.log(randomArray[0]);
//logs: Tahseen
JavaScript 对象是键值对的集合。
键
是对象的特征或属性。同时,值
是相应属性的值。
const Car = {
company: 'Tesla',
model: 'Model 3',
year: 2017
};
JavaScript 对象属性的值可以通过对象名称与属性名称的组合来检索;我们可以在下面的代码段中看到。
console.log(Car.company);
// logs: Tesla
对象数组是一个数组,其中 JavaScript 数组中的每个项目都是一个对象。对象数组类似于任何其他 JavaScript 数组,区别在于每个项目都是一个 JavaScript 对象。
让我们检查以下示例以更好地理解:
const MyAppointments = [
{
"with":"Doctor Tauseef",
"designation":"Dentist",
"reason":"Toothache",
"date": "2021-12-01T06:25:24Z",
},
{
"with":"Abdullah Abid",
"designation":"Software Engineer",
"reason":"An Idea for a App",
"date": "2021-12-01T06:25:24Z",
},
{
"with":"Muhammad Haris",
"designation":"Painter",
"reason":"Need to pain the house",
"date": "2021-13-01T06:25:24Z",
},
]
不带任何参数的 Array.prototype.sort()
用于按升序对数组进行排序。但请记住,该函数将数组元素转换为字符串,并根据它们的 UTF-16 代码单元序列进行比较,因此对于普通用户来说,它可能看起来没有排序。
const Months = ['March', 'Jan', 'Feb', 'Dec'];
Months.sort();
console.log(Months);
// logs: ["Dec", "Feb", "Jan", "March"]
或者,可以将比较函数作为回调函数提供给 array.sort
,它将按照以下标准对数组进行排序:
const Numbers = [4, 2, 5, 1, 3];
Numbers.sort((x, y) => x - y);
console.log(Numbers);
// logs: [1, 2, 3, 4, 5]
你可以使用上面讨论的相同排序方法按日期对对象数组进行排序。
let MyAppointments = [
{
"with":"Doctor Tauseef",
"designation":"Dentist",
"reason":"Toothache",
"appointment_date": "2021-12-01T06:25:24Z",
},
{
"with":"Abdullah Abid",
"designation":"Software Engineer",
"reason":"An Idea for a App",
"appointment_date": "2021-12-09T06:25:24Z",
},
{
"with":"Muhammad Haris",
"designation":"Painter",
"reason":"Need to pain the house",
"appointment_date": "2021-12-05T06:25:24Z",
},
]
MyAppointments.sort(function(x, y) {
var firstDate = new Date(x.appointment_date),
SecondDate = new Date(y.appointment_date);
if (firstDate < SecondDate) return -1;
if (firstDate > SecondDate) return 1;
return 0;
});
console.log(MyAppointments);
如果第一个日期小于第二个日期,这将比较每个日期并返回 -1
。如果第一个日期大于第二个日期,则返回 1
;否则,它将返回 0
。
上面的代码段记录了以下数组。
[
{
with: 'Doctor Tauseef',
designation: 'Dentist',
reason: 'Toothache',
appointment_date: '2021-12-01T06:25:24Z'
},
{
with: 'Muhammad Haris',
designation: 'Painter',
reason: 'Need to pain the house',
appointment_date: '2021-12-05T06:25:24Z'
},
{
with: 'Abdullah Abid',
designation: 'Software Engineer',
reason: 'An Idea for a App',
appointment_date: '2021-12-09T06:25:24Z'
}
]
相关文章
Do you understand JavaScript closures?
发布时间:2025/02/21 浏览次数:108 分类:JavaScript
-
The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.
Do you know about the hidden traps in variables in JavaScript?
发布时间:2025/02/21 浏览次数:178 分类:JavaScript
-
Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av
How much do you know about the Prototype Chain?
发布时间:2025/02/21 浏览次数:150 分类:JavaScript
-
The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start
用 jQuery 检查复选框是否被选中
发布时间:2024/03/24 浏览次数:102 分类:JavaScript
-
在本教程中学习 jQuery 检查复选框是否被选中的所有很酷的方法。我们展示了使用直接 DOM 操作、提取 JavaScript 属性的 jQuery 方法以及使用 jQuery 选择器的不同方法。你还将找到许多有用的
jQuery 中的 Window.onload 与 $(document).ready
发布时间:2024/03/24 浏览次数:180 分类:JavaScript
-
本教程演示了如何在 jQuery 中使用 Window.onload 和 $(document).ready 事件。