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

Posted

技术标签:

【中文标题】我们如何根据节点js中的另一个对象数组值对对象数组进行排序【英文标题】:How can we sort the object array based on another object array value in node js 【发布时间】:2018-09-05 20:12:52 【问题描述】:

我们如何根据另一个对象数组值对对象数组进行排序,就像我们有一个对象数组一样:

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]

在此索引 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]

这里用户 ID 597,605,617,553,616 的得分值为 85,95,85,100,95

所以根据分数,我想对我的第一个数组进行排序 输出应该是:

["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]

【问题讨论】:

看看在developer.mozilla.org/en-US/docs/Web/javascript/Reference/…上使用 compareFunction 的例子 【参考方案1】:

您可以获取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; 

【讨论】:

一如既往的优雅 :) +1【参考方案2】:

使用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

演示

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);

【讨论】:

【参考方案3】:

通过userID 使用Array.reduce 创建score 的对象。按tEnd 排序,如果相等,则按score 排序,您从sortObject 得到userId

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);

【讨论】:

以上是关于我们如何根据节点js中的另一个对象数组值对对象数组进行排序的主要内容,如果未能解决你的问题,请参考以下文章

如何将一个对象插入到mongo数组中的另一个对象中

(去重)JS比较两个数组对象,取出不同的值

怎么对数组中的对象去重

ES6 数组内对象去重

ReactJS:如何根据道具的值对对象数组进行排序?

在数组Vue Js中的另一个不同json对象中具有相同值的数组中的所有json对象中添加/合并新项目