简体   繁体   English

如何对这样格式化的数组进行排序?

[英]How do I sort an array formatted like this?

I was trying to sort an array like this by "walls": 我试图通过“ walls”对这样的数组进行排序:

[{
  "536606220753305611": { "walls": 4 },
  "137616812886982656": { "walls": 5 },
  "189862971520843776": { "walls": 4 },
  "230433188491558913": { "walls": 2 }
}]

I can't seem to access the array from within the ids with array.sort. 我似乎无法使用array.sort从id内访问数组。

Here's what I've tried: 这是我尝试过的:

let top10 = array.sort(function(a,b){
  return a.walls.localeCompare(b.walls).slice(0, 10);
 });

It won't work here because I can't find how to get inside the id arrays. 它在这里不起作用,因为我找不到如何进入id数组的内部。

I expect the result I logged to be in order, but it shows this instead: 我希望我记录的结果是按顺序排列的,但是却显示如下:

[ { '536606220753305611': { walls: 4 },
    '137616812886982656': { walls: 5 },
    '189862971520843776': { walls: 4 },
    '230433188491558913': { walls: 2 } } ]

Thanks! 谢谢!

Codingpro 编码软件

If the desired output is an object with ordered keys, that can't be done (JS objects' properties are inherently unordered). 如果所需的输出是带有有序键的对象,则无法完成(JS对象的属性本质上是无序的)。 You can first transform the input into an array of objects, then sort those.... 可以先将输入转换为对象数组,然后对它们进行排序。

 let data = [{ "536606220753305611": { "walls": 4 }, "137616812886982656": { "walls": 5 }, "189862971520843776": { "walls": 4 }, "230433188491558913": { "walls": 2 } }] // transform the input object into an array of objects of the form: // // [ { "long string of digits" : { walls : 4 } }, // { "long string of digits" : { walls : 2 } }, // etc. // ] let data0 = data[0]; let objects = Object.keys(data0).map(k => { return { k: data0[k] } }); let sorted = objects.sort((a, b) => { let keyA = Object.keys(a)[0]; let keyB = Object.keys(b)[0]; return a[keyA].walls - b[keyB].walls; }); console.log(sorted) 

What you're trying to do isn't possible with your current data structure. 您当前的数据结构无法实现您想做的事情。

You basically have one element in your array that you're trying to sort... which by default is already sorted. 您的数组中基本上有一个要排序的元素...默认情况下已排序。 And it isn't possible to sort the keys in an object as they do not have any specific order. 是不可能的,因为他们没有任何特定的顺序中的对象键进行排序。

For your example to work, you need to create multiple elements in your your list to sort. 为了使示例生效,您需要在列表中创建多个元素进行排序。 Here is an example of what you could do. 这是您可以做什么的示例。

 const data = [{ "536606220753305611": { "walls": 4 } }, { "137616812886982656": { "walls": 5 } }, { "189862971520843776": { "walls": 4 } }, { "230433188491558913": { "walls": 2 } }]; const res = data.sort((a,b)=>{ return Object.values(a).shift().walls - Object.values(b).shift().walls; }); console.log(res); 

Here you have another approach that use the experimental flatMap() and will also work if you have one initial array with multiple objects containing data: 在这里,您有另一种使用实验性flatMap()的方法,并且如果您具有一个包含多个包含数据的对象的初始数组,则该方法也将起作用:

 const input = [ { "536606220753305611": { "walls": 4 }, "137616812886982656": { "walls": 5 }, "189862971520843776": { "walls": 4 }, "230433188491558913": { "walls": 2 } }, { "336606220753305611": { "walls": 9 }, "437616812886982656": { "walls": 7 }, "789862971520843776": { "walls": 23 }, "90433188491558913": { "walls": 1 } } ]; let res = input.flatMap(e => Object.entries(e)) .sort((a, b) => a[1].walls - b[1].walls) .map(([key, value]) => ({[key]: value})); console.log(res); 

If you wanted to keep a similar structure but sort the keys/values you could always swap your array for a Map instead. 如果您想保留相似的结构但对键/值进行排序,则可以始终将数组替换为Map

 const data = [{ "536606220753305611": { "walls": 4 }, "137616812886982656": { "walls": 5 }, "189862971520843776": { "walls": 4 }, "230433188491558913": { "walls": 2 } }]; // Create a new Map from your existing data const map = new Map(Object.entries(data[0])); // Create an array of Map entries sorted by wall value // and then create a new Map from that array const sortedMap = new Map([...map.entries()].sort((a, b) => { return a[1].walls < b[1].walls; })); // Voila, a sorted Map for (let [k, v] of sortedMap.entries()) { console.log(k, v); } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM