当前位置:网站首页>[JS sort according to the attributes in the object array]
[JS sort according to the attributes in the object array]
2022-07-05 09:28:00 【Guodong pudding】
【js Sort according to the attributes in the object array 】
var arr = [
{name:'a',age:9},
{name:'b',age:12},
{name:'c',age:8}
];
arr.sort((a,b)=>{
return a.age - b.age
});
function compare(property){
return function(a,b){
var value1 = a[property];
var value2 = b[property];
return value1 - value2;
}
}
console.log(arr.sort(compare('age')))
* Examples of use :newArray.sort(sortBy('number',false)) // Express basis number Attribute descending ; If the second parameter is not passed , Default means ascending sort
* @param attr The properties of sorting Such as number attribute
* @param rev true Indicates ascending order ,false null
* */
sortBy: function(attr,rev){
// The second parameter is not passed Default ascending order
if(rev == undefined){
rev = 1;
}else{
rev = (rev) ? 1 : -1;
}
return function(a,b){
a = a[attr];
b = b[attr];
if(a < b){
return rev * -1;
}
if(a > b){
return rev * 1;
}
return 0;
}
}
边栏推荐
- 2311. Longest binary subsequence less than or equal to K
- LeetCode 556. Next bigger element III
- 【js 根据对象数组中的属性进行排序】
- C # compare the differences between the two images
- 深入浅出PyTorch中的nn.CrossEntropyLoss
- 【PyTorch Bug】RuntimeError: Boolean value of Tensor with more than one value is ambiguous
- Understanding of smt32h7 series DMA and DMAMUX
- Deep understanding of C language pointer
- 【sourceTree配置SSH及使用】
- OpenGL - Lighting
猜你喜欢

Applet global style configuration window

C form click event did not respond

Introduction Guide to stereo vision (7): stereo matching
![[ctfhub] Title cookie:hello guest only admin can get flag. (cookie spoofing, authentication, forgery)](/img/78/d9d1a66fc239e7c62de1fce426d30d.jpg)
[ctfhub] Title cookie:hello guest only admin can get flag. (cookie spoofing, authentication, forgery)
![Rebuild my 3D world [open source] [serialization-1]](/img/74/b6253845b43bc18f425d57695fba7c.jpg)
Rebuild my 3D world [open source] [serialization-1]

Principle and performance analysis of lepton lossless compression

Unity skframework framework (XXIII), minimap small map tool

Nodejs modularization

AUTOSAR from getting started to mastering 100 lectures (103) -dbc file format and creation details
![Introduction Guide to stereo vision (4): DLT direct linear transformation of camera calibration [recommended collection]](/img/ed/0483c529db2af5b16b18e43713d1d8.jpg)
Introduction Guide to stereo vision (4): DLT direct linear transformation of camera calibration [recommended collection]
随机推荐
Cloud computing technology hotspot
【两个对象合并成一个对象】
OpenGL - Lighting
Ministry of transport and Ministry of Education: widely carry out water traffic safety publicity and drowning prevention safety reminders
[technical school] spatial accuracy of binocular stereo vision system: accurate quantitative analysis
【el-table如何禁用】
Transfer learning and domain adaptation
Confusion matrix
Applet customization component
22-07-04 Xi'an Shanghao housing project experience summary (01)
我的一生.
初识结构体
Deep understanding of C language pointer
Unity skframework framework (XXII), runtime console runtime debugging tool
OpenGL - Coordinate Systems
SMT32H7系列DMA和DMAMUX的一点理解
NIPS2021 | 超越GraphCL,GNN+对比学习的节点分类新SOTA
【对象数组的排序】
L'information et l'entropie, tout ce que vous voulez savoir est ici.
Nips2021 | new SOTA for node classification beyond graphcl, gnn+ comparative learning