[英]Sort a key with optional object
I have the following data as Input and I am trying to get the desired output as a result of sorting the data:我有以下数据作为输入,并且由于对数据进行排序,我试图获得所需的 output:
Input输入
[
{
"info": { "name": "abc" }
},
{
"info": { "name": "def" }
},
{
"info": {},
"stats": { "packages": 10, "score": 10 }
},
{
"info": {},
"stats": { "packages": 5, "score": 8 }
},
{
"info": {},
"stats": { "packages": 7, "score": 6 }
}
]
Desired Output - Sort by packages descending, if present所需的 Output - 按包降序排序(如果存在)
[
{
"info": {},
"stats": { "packages": 10, "score": 10 }
},
{
"info": {},
"stats": { "packages": 7, "score": 6 }
},
{
"info": {},
"stats": { "packages": 5, "score": 8 }
},
{
"info": { "name": "abc" }
},
{
"info": { "name": "def" }
}
]
So far I have this code to filter data that has stats object, apply sort on it and then combine it with data that does not stats object.到目前为止,我有这段代码来过滤具有统计数据 object 的数据,对其应用排序,然后将其与不统计 object 的数据组合。 I am wondering if there's an efficient way to do this.
我想知道是否有一种有效的方法来做到这一点。
filterData = [];
sortedData = [];
filterData = [...filterData, ...data.filter(x => x.stats)];
filterData.sort((a, b) => b.stats.packages.localeCompare(a.stats.packages));
sortedData = [...filterData, ...data.filter(x => !x.stats)];
You need to define a custom sort function.您需要定义一个自定义排序 function。 A sort function takes in two arguments (elements from the array) and returns either a positive, negative, or zero value, depending on which element is considered bigger.
排序 function 接受两个 arguments(数组中的元素)并返回正值、负值或零值,具体取决于哪个元素被认为更大。 Read about it more here: Array.prototype.sort() .
在这里阅读更多内容: Array.prototype.sort() 。
Next up, to see if the package
variable exists you can use Optional_chaining接下来,要查看
package
变量是否存在,您可以使用Optional_chaining
const arr = [ { "info": { "name": "abc" } }, { "info": { "name": "def" } }, { "info": {}, "stats": { "packages": 10, "score": 10 } }, { "info": {}, "stats": { "packages": 5, "score": 8 } }, { "info": {}, "stats": { "packages": 7, "score": 6 } } ] const sorted = arr.sort((item1, item2) => (item2?.stats?.packages || 0) - (item1?.stats?.packages || 0) ) console.log(sorted)
If you do not have hundreds of data it should be efficient enough.如果您没有数百个数据,它应该足够高效。 Alternatively, you can use or write sort function like that:
或者,您可以像这样使用或编写排序 function :
data.sort((a, b) => (b.stats?.packages ?? 0) - (a.stats?.packages ?? 0))
Using Array#sort
, optinal-chaining , and nullish-operator :使用
Array#sort
、 optinal -chaining和nullish-operator :
const data = [ { "info": { "name": "abc" } }, { "info": { "name": "def" } }, { "info": {}, "stats": { "packages": 10, "score": 10 } }, { "info": {}, "stats": { "packages": 5, "score": 8 } }, { "info": {}, "stats": { "packages": 7, "score": 6 } } ]; const sorted = data.sort((a, b) => (b?.stats?.packages?? 0) - (a?.stats?.packages?? 0) ); console.log(sorted);
data = [ { "info": { "name": "abc" } }, { "info": { "name": "def" } }, { "info": {}, "stats": { "packages": 10, "score": 10 } }, { "info": {}, "stats": { "packages": 5, "score": 8 } }, { "info": {}, "stats": { "packages": 7, "score": 6 } } ] const sorter = (a,b)=>{ let aa = a.stats?a.stats.packages:1; let bb = b.stats?b.stats.packages:1; return bb - aa; } data.sort(sorter); console.log(data)
hum.. like this?嗯..像这样?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.