js对象数组按照另一个数组排序
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了js对象数组按照另一个数组排序相关的知识,希望对你有一定的参考价值。
参考技术A 场景下拉列表有4个选项
用户按照先后排序后保存
[2, 4, 1, 3]
保存完后,按照顺序进行排序
思路,两个数组,排序有点困难
如果变成一个数组排序,就方便了
我们给每个option新增一个排序属性sordId
这个sordId的值就是 option的id在 showArr中的索引位置
现在我们的options数组已经可以排序了
再写一个对象数组按照属性名排序的方法
最后输出
我们如何根据节点js中的另一个对象数组值对对象数组进行排序
【中文标题】我们如何根据节点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】:使用sort
和find
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对象数组按照另一个数组排序的主要内容,如果未能解决你的问题,请参考以下文章