按键,然后按命名属性对Javascript对象进行排序 [英] Sort a Javascript Object by Key, then by named property

查看:94
本文介绍了按键,然后按命名属性对Javascript对象进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法按KEY(DESC)对以下对象进行排序. 然后,通过"notesList.id"属性(DESC)对每个项目进行排序.

I am having trouble into sorting the following Object by KEY (DESC). Then, sorting each item by the "notesList.id" property (DESC).

  • "2017/04/17"
  • "2017/03/14"
  • "2017/02/30"

然后,针对"2017/04/17"注释列出项目,这是预期订单:

Then, for the "2017/04/17" notesList items, here is the Expected order :

  • id:57
  • id:48
  • id:12
var myNotes = {
  "customNotes":{
    "2017/04/17":{
      "concernedDate":"2017/04/17",
      "notesList":[
        {
          "id":48,
          "title":"Title 48"
        },
        {
          "id":12,
          "title":"Title 12"
        },
        {
          "id":57,
          "title":"Title 57"
        }
      ]
    },
    "2017/02/30":{
      "concernedDate":"2017/02/30",
      "notesList":[
      {
        "id":92,
        "title":"Title 92"
      }
      ]
    },
    "2017/03/14":{
      "concernedDate":"2017/03/14",
      "notesList":[
      {
        "id":92,
        "title":"Title 92"
      }
      ]
    }
  }
}

脚本

//Convert the OBJECT to an ARRAY
var tmpArr = Object.keys(myNotes.customNotes);

//Sort (Not working)
tmpArr.sort((a, b) => a > b ? a : b);

//Convert back the ARRAY to an OBJECT
Object.keys(myNotes.customNotes).sort((a, b) => a > b ? a : b).map(k => myNotes.customNotes[k]);

我想念什么?我非常感谢你

What am I missing ? I thank you very much

推荐答案

我发现您的代码中似乎有些地方不合时宜.

I observe a few things that seem out of place in your code.

  1. tmpArr.sort((a, b) => a > b ? a : b);返回tmpArr的排序版本.它不会修改myNotes对象.
  2. Object.keys(myNotes.customNotes).sort(...将从myNotes.customNotes返回一个对象数组,而不是像您想要的那样具有与myNotes具有相同结构的对象.
  3. 在上面的代码中,您没有对notesList进行排序.
  1. tmpArr.sort((a, b) => a > b ? a : b); returns a sorted version of tmpArr. it will not modify myNotes object.
  2. Object.keys(myNotes.customNotes).sort(... will return an array of objects from myNotes.customNotes, and not an object that has the same structure as myNotes like you intended.
  3. In the code above, you are not sorting notesList.

假设您要获取一个数组,您所需要做的就是修复您的排序函数,并在map函数中对notesList进行排序.因此,您的代码应如下所示:

Suppose you want to to get an array, all you need is to fix your sort function and also sort your notesList in the map function. So your code should look like this:

var myNotes={customNotes:{"2017/04/17":{concernedDate:"2017/04/17",notesList:[{id:48,title:"Title 48"},{id:12,title:"Title 12"},{id:57,title:"Title 57"}]},"2017/02/30":{concernedDate:"2017/02/30",notesList:[{id:92,title:"Title 92"}]},"2017/03/14":{concernedDate:"2017/03/14",notesList:[{id:92,title:"Title 92"}]}}};

var res = Object.keys(myNotes.customNotes)
  .sort((a, b) => a < b)
  .map(k => {   
    let currNote = myNotes.customNotes[k];
    currNote.notesList.sort((a, b) => a.id < b.id)
    return myNotes.customNotes[k];
});

console.log(res);

但是,如果您打算保留myNotes的结构,建议您使用reduce函数.请参阅下面的代码:

But, if you intend to keep the structure of myNotes, I would suggest that you use reduce function. See my code below:

var myNotes={customNotes:{"2017/04/17":{concernedDate:"2017/04/17",notesList:[{id:48,title:"Title 48"},{id:12,title:"Title 12"},{id:57,title:"Title 57"}]},"2017/02/30":{concernedDate:"2017/02/30",notesList:[{id:92,title:"Title 92"}]},"2017/03/14":{concernedDate:"2017/03/14",notesList:[{id:92,title:"Title 92"}]}}};

// MY SOLUTION
var sortedNotes = Object.keys(myNotes.customNotes)
  .sort((a, b) => a < b)
  .reduce((acc, val, _, arr) => { 
    acc[val] = myNotes.customNotes[val];
    acc[val].notesList.sort((a, b) => a.id < b.id);
    return acc;  
},{});

console.log(sortedNotes);

这篇关于按键,然后按命名属性对Javascript对象进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆