如何在Javascript中对嵌套JSON对象数组中的日期、数字和字符串进行排序
Hi动态获取JOSN数据,基于JOSN将在表中呈现数据。在JSON中,嵌套对象出现,这些名称和属性键也动态地出现。我需要根据表列up和down button action ASC和DESC进行排序。在button action上,我获得要排序的属性名称,属性键名称将放置在嵌套对象中,或者可能位于上层。如何动态识别和排序数字、字符串或日期。感谢 在逻辑下面,我只添加了用于单级JSON对象的工作,而不是嵌套级对象如何在Javascript中对嵌套JSON对象数组中的日期、数字和字符串进行排序,javascript,json,reactjs,ecmascript-6,ecmascript-5,Javascript,Json,Reactjs,Ecmascript 6,Ecmascript 5,Hi动态获取JOSN数据,基于JOSN将在表中呈现数据。在JSON中,嵌套对象出现,这些名称和属性键也动态地出现。我需要根据表列up和down button action ASC和DESC进行排序。在button action上,我获得要排序的属性名称,属性键名称将放置在嵌套对象中,或者可能位于上层。如何动态识别和排序数字、字符串或日期。感谢 在逻辑下面,我只添加了用于单级JSON对象的工作,而不是嵌套级对象 dynamicJSON.sort((a, b) => { i
dynamicJSON.sort((a, b) => {
if (a[sortKeyname] < b[sortKeyname]) {
return sortConfig.direction === 'ascending' ? -1 : 1;
}
if (a[sortKeyname] > b[sortKeyname]) {
return sortConfig.direction === 'ascending' ? 1 : -1;
}
return 0;
});
您可以对想要的键和方向使用闭包,并使用返回的函数进行排序 排序函数使用一个函数,该函数将移交的键字符串按点拆分,并从嵌套对象中获取值 常数 sortBy=键,方向='升序'=>a,b=>{ 常数 因子=+方向=='上升'| |-1, getValue=object,keys=>keys.split'.。reduceo,k=>o?[k],object, valueA=getValuea,键, valueB=getValueb,键; 返回因子*valueA>valueB | |-valueA
x来进行排序,还是只是尝试对组件中的整个内容进行排序?我想澄清一下,一次只能对单个属性进行排序。你的问题是这样的。我总是觉得从你想要排序的属性中构建一个复合值是简单而直接的。当值包括数字和日期时,这会更困难,但并非不可能。解决方案更接近,降序是如何工作的?对,降序排序缺少一些值。请参阅编辑。我已尝试,降序无效。在逻辑中,降序活动在何处处理?因子为1或-1以确定方向。您能否详细说明升序和降序的逻辑?解决方案更接近,降序如何工作?对,降序排序缺少一些值。请参阅编辑。我已尝试,降序无效。在逻辑中下行活动在哪里处理?形成方向的因子为1或-1。您能详细说明上行和下行的逻辑吗?
[
{
"Date":"2020-10-16T04:15:58+00:00",
"applicationName":"abc Portal",
"status":{
"statusname":"Success",
"style":"success"
},
"details":{
"maindescription":"welcome to the application",
"maindescriptionSecn":"description 2",
"maindescriptionSecnthrid":"description 3"
},
"location":"Sondekoppa, Karnataka, IN",
"ipAddress":"157.49.147.190",
"count":123
},
{
"Date":"2020-10-16T04:15:56+00:00",
"applicationName":"poratl 1",
"status":{
"statusname":"Success",
"style":"success"
},
"details":{
"maindescription":"welcome to the application",
"maindescriptionSecn":"description 2",
"maindescriptionSecnthrid":"description 3"
},
"location":"Sondekoppa, Karnataka, IN",
"ipAddress":"157.49.147.190",
"count":789
},
{
"Date":"2020-10-16T04:21:41+00:00",
"applicationName":"app Hub",
"status":{
"statusname":"Failure",
"style":"error"
},
"details":{
"maindescription":"welcome to the application",
"maindescriptionSecn":"description 2",
"maindescriptionSecnthrid":"description 3"
},
"location":"Sondekoppa, Karnataka, IN",
"ipAddress":"157.49.147.190",
"count":666
}
]