是否可以为其值对象的属性对 ES6 映射对象进行排序?

我有一个对象值的映射。我想根据其对象值的属性对它们进行排序。


这是示例地图:


var map = new Map();

map.set("8014",{

            "Access to support": null,

            "Capacity":  1,

            "Comfort Level": null,

            "Control Level": 1,

            "Growth Mindset": 1.5405405405405403,

            "Horizon": null,

            "Motivation": null,

            "Open-Mindedness": null,

            "Proactivity": null,

            "Resiliency": null,

            "Segment": "Level value 1"});

map.set("8015",{

            "Access to support": null,

            "Capacity":  1,

            "Comfort Level": null,

            "Control Level": 1,

            "Growth Mindset": 1.5405405405405403,

            "Horizon": null,

            "Motivation": null,

            "Open-Mindedness": null,

            "Proactivity": null,

            "Resiliency": null,

            "Segment": "Level value 2 or 5"});

map.set("8016",{

            "Access to support": null,

            "Capacity":  1,

            "Comfort Level": null,

            "Control Level": 1,

            "Growth Mindset": 1.5405405405405403,

            "Horizon": null,

            "Motivation": null,

            "Open-Mindedness": null,

            "Proactivity": null,

            "Resiliency": null,

            "Segment": "Level value 3 or 5"});

我想根据值对象的 Segment 属性对其进行排序。任何帮助将不胜感激!


慕勒3428872
浏览 277回答 2
2回答

隔江千里

您可以使用sort基于 Map 的条目。然后从排序的条目创建一个新的 MapSegmentlocaleComparevar map=new Map();map.set("8014",{"Access to support":null,Capacity:1,"Comfort Level":null,"Control Level":1,"Growth Mindset":1.5405405405405403,Horizon:null,Motivation:null,"Open-Mindedness":null,Proactivity:null,Resiliency:null,Segment:"Level value 1"});map.set("8015",{"Access to support":null,Capacity:1,"Comfort Level":null,"Control Level":1,"Growth Mindset":1.5405405405405403,Horizon:null,Motivation:null,"Open-Mindedness":null,Proactivity:null,Resiliency:null,Segment:"Level value 2 or 5"});map.set("8016",{"Access to support":null,Capacity:1,"Comfort Level":null,"Control Level":1,"Growth Mindset":1.5405405405405403,Horizon:null,Motivation:null,"Open-Mindedness":null,Proactivity:null,Resiliency:null,Segment:"Level value 3 or 5"});const sortedMap =  new Map(    Array.from(map)        .sort((a, b) => a[1].Segment.localeCompare(b[1].Segment))  );console.log([...sortedMap.entries()])
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript