Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/460.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 对象已排序,但未使用lodash sortBy保留其键_Javascript_Arrays_Lodash - Fatal编程技术网

Javascript 对象已排序,但未使用lodash sortBy保留其键

Javascript 对象已排序,但未使用lodash sortBy保留其键,javascript,arrays,lodash,Javascript,Arrays,Lodash,上下文 { "Inventory Creation": [ { "id": 150, "reading": "12345", "readingDate": "2020-07-14" } ], "Inventory & Check-in": [

上下文

{
    "Inventory Creation": [
      {
        "id": 150,
        "reading": "12345",
        "readingDate": "2020-07-14"
      }
    ],
    "Inventory & Check-in": [
      {
        "id": 151,
        "reading": "12345",
        "readingDate": "2020-11-14"
      }
    ],
    "Check-in": [
      {
        "id": 152,
        "reading": "12345",
        "readingDate": "2020-08-14"
      }
    ]
}
{
    "Inventory & Check-in": [
      {
        "id": 151,
        "reading": "12345",
        "readingDate": "2020-11-14"
      }
    ],
    "Check-in": [
      {
        "id": 152,
        "reading": "12345",
        "readingDate": "2020-08-14"
      }
    ],
    "Inventory Creation": [
      {
        "id": 150,
        "reading": "12345",
        "readingDate": "2020-07-14"
      }
    ]
}
需要使用lodash按
readingDate
对以下对象进行排序,我可以对其进行排序,但它的索引没有保留

原始JSON对象

{
    "Inventory Creation": [
      {
        "id": 150,
        "reading": "12345",
        "readingDate": "2020-07-14"
      }
    ],
    "Inventory & Check-in": [
      {
        "id": 151,
        "reading": "12345",
        "readingDate": "2020-11-14"
      }
    ],
    "Check-in": [
      {
        "id": 152,
        "reading": "12345",
        "readingDate": "2020-08-14"
      }
    ]
}
{
    "Inventory & Check-in": [
      {
        "id": 151,
        "reading": "12345",
        "readingDate": "2020-11-14"
      }
    ],
    "Check-in": [
      {
        "id": 152,
        "reading": "12345",
        "readingDate": "2020-08-14"
      }
    ],
    "Inventory Creation": [
      {
        "id": 150,
        "reading": "12345",
        "readingDate": "2020-07-14"
      }
    ]
}
我尝试过的代码

_.sortBy(unsorted, o => o[0].readingDate).reverse();
此处
unsorted
包含上述原始JSON对象

实际结果(我得到的)

预期结果(我想要的)


这是集合/数组的组合。查看您正在使用的lodash函数及其用途。这无法工作,您正在尝试使用对象的数组函数。该函数执行它应该执行的操作,对数组进行排序。但是没有办法以这种方式创建排序对象,它(意味着lodash函数)甚至不知道该对象

话虽如此,这里有一个有效的解决方案(总体上看map/reduce):

const entries=Object.entries(未排序);
const sorted=entries.sort((a,b)=>a[1][0]。读取日期{
acc[obj[0]]=obj[1]
返回acc},{});
Ofc您可以链接这些电话,但我试图让它更容易理解。
我刚刚偶然发现了哪些也可以使用。

以下是步骤:

  • 将对象转换为键值对列表(
    .toPairs()
    或内置的
    对象.entries()
  • 对列表排序
  • 将键值对列表转换回对象(
    .fromPairs()
    或内置的
    对象.fromEntries()
下面的代码片段可以帮助您。为了更好的可读性,我使用了
\uuu.chain()

const数据={
“创建库存”:[
{
id:150,
“12345”,
阅读日期:“2020-07-14”,
},
],
“库存和登记”:[
{
id:151,
“12345”,
阅读日期:“2020-11-14”,
},
],
“签入”:[
{
身份证号码:152,
“12345”,
阅读日期:“2020-08-14”,
},
],
}
const res=uu.chain(数据)
.toPairs()
.sortBy((p)=>p[1][0]。读取日期)
.reverse()
.fromPairs()
.value()
console.log(res)

这里是一个暴力解决方案。首先对数组进行排序,并存储键。然后,使用SorterDarray中的键形成数组

var sortedArray = _.sortBy(unsortedArray, function(val, key){
    val[0].key = key;
    return val[0].readingDate;
}).reverse();

var sortedArrayWithIndex = {};

_.each(sortedArray, function(val){
    var key = val[0].key;
    delete val[0].key;
    sortedArrayWithIndex[key] = val;
});
console.log(sortedArrayWithIndex);

您的原始JSON对象实际上是一个对象,而不是一个数组,
在排序后返回一个数组。你为什么要对一个对象进行排序?谢谢你的更正,让我更新这个问题,让它更容易理解。没问题,尽管我仍然不明白你为什么要对一个对象进行排序。实际上这是我正在处理的任务的要求,我需要在列表中显示上面的JSON对象,这个列表应该按照
readingDate
进行排序,我明白了,那么请注意JavaScript并不总是保证对象键的顺序,特别是当这些键在字符串和数字之间混合时,如前所述。很好的建议,值得注意的是,这(或任何其他方法)如果键具有混合类型,则不起作用。例如,如果我们将键
“Check-in”
更改为
1
,那么最终的输出是错误的,因为对象在JS中是如何工作的。