简体   繁体   English

我们如何基于节点js中的另一个对象数组值对对象数组进行排序

[英]How can we sort the object array based on another object array value in node js

How can we sort the object array based on another object array value, like we have an object array: 我们如何基于另一个对象数组值对对象数组进行排序,就像我们有一个对象数组一样:

var _userEnd = [{"userID":554,"tEnd":6},{"userID":597,"tEnd":3},{"userID":605,"tEnd":3},{"userID":617,"tEnd":1},{"userID":553,"tEnd":1},{"userID":616,"tEnd":1},{"userID":596,"tEnd":0}]

In this the index 1,2,3,4,5 are having the same value of the key "tEnd" ie, 3 and 1 so I just need to sort only those index on the basis of another object array 在这种情况下,索引1,2,3,4,5具有相同的键“ tEnd”值,即3和1,因此我只需要根据另一个对象数组对那些索引进行排序

var _profsort=[{"userID":596,"score":100},{"userID":616,"score":95},{"userID":553,"score":100},{"userID":617,"score":85},{"userID":605,"score":95},{"userID":597,"score":85},{"userID":554,"score":100}]

Here the userID 597,605,617,553,616 are having the score value 85,95,85,100,95 这里的用户597,605,617,553,616具有得分值85,95,85,100,95

so based on score, I want to sort my first array Output should be: 所以根据分数,我想对我的第一个数组进行排序输出应该是:

[{"userID":554,"tEnd":6},{"userID":605,"tEnd":3},{"userID":597,"tEnd":3},{"userID":553,"tEnd":1},{"userID":616,"tEnd":1},{"userID":617,"tEnd":1},{"userID":596,"tEnd":0}]

You could take the delta of tEnd and find the score and take that delta. 您可以采用tEnd的增量,找到score并采用该增量。

 function getScore(uID) { return (_profsort.find(({ userID }) => userID === uID) || { score: 0 }).score; } var _userEnd = [{ userID: 554, tEnd: 6 }, { userID: 597, tEnd: 3 }, { userID: 605, tEnd: 3 }, { userID: 617, tEnd: 1 }, { userID: 553, tEnd: 1 }, { userID: 616, tEnd: 1 }, { userID: 596, tEnd: 0 }], _profsort = [{ userID: 596, score: 100 }, { userID: 616, score: 95 }, { userID: 553, score: 100 }, { userID: 617, score: 85 }, { userID: 605, score: 95 }, { userID: 597, score: 85 }, { userID: 554, score: 100 }]; _userEnd.sort(function (a, b) { return b.tEnd - a.tEnd || getScore(b.userID) - getScore(a.userID); }); console.log(_userEnd); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

Use sort and find 使用sortfind

var fnGetScore = uid => _profsort.find(s => s.userID == uid).score; //method to get the score value based on userId

var output = _userEnd.sort( (a, b) => 
        (b.tEnd - a.tEnd) || 
           fnGetScore(b.userID) - fnGetScore(b.userID)) //compare tEnd first and if they are same, compare the score value

Demo 演示

 var _userEnd = [{ "userID": 554, "tEnd": 6 }, { "userID": 597, "tEnd": 3 }, { "userID": 605, "tEnd": 3 }, { "userID": 617, "tEnd": 1 }, { "userID": 553, "tEnd": 1 }, { "userID": 616, "tEnd": 1 }, { "userID": 596, "tEnd": 0 }]; var _profsort = [{ "userID": 596, "score": 100 }, { "userID": 616, "score": 95 }, { "userID": 553, "score": 100 }, { "userID": 617, "score": 85 }, { "userID": 605, "score": 95 }, { "userID": 597, "score": 85 }, { "userID": 554, "score": 100 }]; var fnGetScore = uid => _profsort.find(s => s.userID == uid).score; var output = _userEnd.sort((a, b) => (b.tEnd - a.tEnd) || fnGetScore(b.userID) - fnGetScore(b.userID)) console.log(output); 

Create an object of score by userID using Array.reduce . 使用Array.reduce通过userID创建一个score对象。 Sort by tEnd , and if it's equal sort by the score you get from the sortObject by the userId : 排序方式tEnd ,如果它是等于排序的score ,你从一开始sortObjectuserId

 var _userEnd = [{"userID":554,"tEnd":6},{"userID":597,"tEnd":3},{"userID":605,"tEnd":3},{"userID":617,"tEnd":1},{"userID":553,"tEnd":1},{"userID":616,"tEnd":1},{"userID":596,"tEnd":0}] var _profsort=[{"userID":596,"score":100},{"userID":616,"score":95},{"userID":553,"score":100},{"userID":617,"score":85},{"userID":605,"score":95},{"userID":597,"score":85},{"userID":554,"score":100}] var sortObj = _profsort.reduce((r, o) => { r[o.userID] = o.score; return r; }, Object.create(null)); _userEnd.sort((a, b) => b.tEnd - a.tEnd || sortObj[b.userID] - sortObj[a.userID]); console.log(_userEnd); 

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

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