admin管理员组文章数量:1399013
I have the following array of unique IDs:
idArray = ["56f4cf96dd2ca7275feaf802",
"56f4cf96dd2ca7275feaf7b7",
"56f4cf96dd2ca7275feaf805",
"56f4cf96dd2ca7275feaf7ac"]
And I have another array of objects:
stories = [{"title": Story2, id = "56f4cf96dd2ca7275feaf7b7"},
{"title": Story4, id = "56f4cf96dd2ca7275feaf7ac"},
{"title": Story1, id = "56f4cf96dd2ca7275feaf802"},
{"title": Story3, id = "56f4cf96dd2ca7275feaf805"}]
How can I sort the second array based on the index of the first array? Preferably using lodash, as the arrays can grow a bit larger.
So far, I have the following to get the indexes from the first array:
var sortArray = _.toPairs(idArray)
[ [ '0', 56f4cf96dd2ca7275feaf802 ],
[ '1', 56f4cf96dd2ca7275feaf7b7 ],
[ '2', 56f4cf96dd2ca7275feaf805 ],
[ '3', 56f4cf96dd2ca7275feaf7ac ] ]
Trying different binations of _.map() and _.sortBy() I can't seem to properly get the result I want which is:
desiredResult = [{"title": Story1, id = "56f4cf96dd2ca7275feaf802"},
{"title": Story2, id = "56f4cf96dd2ca7275feaf7b7"},
{"title": Story3, id = "56f4cf96dd2ca7275feaf805"},
{"title": Story4, id = "56f4cf96dd2ca7275feaf7ac"}]
I have the following array of unique IDs:
idArray = ["56f4cf96dd2ca7275feaf802",
"56f4cf96dd2ca7275feaf7b7",
"56f4cf96dd2ca7275feaf805",
"56f4cf96dd2ca7275feaf7ac"]
And I have another array of objects:
stories = [{"title": Story2, id = "56f4cf96dd2ca7275feaf7b7"},
{"title": Story4, id = "56f4cf96dd2ca7275feaf7ac"},
{"title": Story1, id = "56f4cf96dd2ca7275feaf802"},
{"title": Story3, id = "56f4cf96dd2ca7275feaf805"}]
How can I sort the second array based on the index of the first array? Preferably using lodash, as the arrays can grow a bit larger.
So far, I have the following to get the indexes from the first array:
var sortArray = _.toPairs(idArray)
[ [ '0', 56f4cf96dd2ca7275feaf802 ],
[ '1', 56f4cf96dd2ca7275feaf7b7 ],
[ '2', 56f4cf96dd2ca7275feaf805 ],
[ '3', 56f4cf96dd2ca7275feaf7ac ] ]
Trying different binations of _.map() and _.sortBy() I can't seem to properly get the result I want which is:
desiredResult = [{"title": Story1, id = "56f4cf96dd2ca7275feaf802"},
{"title": Story2, id = "56f4cf96dd2ca7275feaf7b7"},
{"title": Story3, id = "56f4cf96dd2ca7275feaf805"},
{"title": Story4, id = "56f4cf96dd2ca7275feaf7ac"}]
Share
Improve this question
asked May 30, 2016 at 22:49
user1202888user1202888
1,0432 gold badges11 silver badges20 bronze badges
3 Answers
Reset to default 4I believe the sort solution is very ineffective especially since you expect the arrays to grow bigger later. Sort is "at best" an O(2n) operation while you have two indexOf operations per cycle another O(2n^2). I propose the following which will outperform the sort method in large arrays.
var stories = [{"title": 'Story2', id : "56f4cf96dd2ca7275feaf7b7"},
{"title": 'Story4', id : "56f4cf96dd2ca7275feaf7ac"},
{"title": 'Story1', id : "56f4cf96dd2ca7275feaf802"},
{"title": 'Story3', id : "56f4cf96dd2ca7275feaf805"}],
idArray = ["56f4cf96dd2ca7275feaf802",
"56f4cf96dd2ca7275feaf7b7",
"56f4cf96dd2ca7275feaf805",
"56f4cf96dd2ca7275feaf7ac"],
ordered = idArray.reduce((p,c) => p.concat(stories.find(f => f.id == c)) ,[]);
console.log(ordered);
Only O(n^2)
This is possible to do without any library using Array.sort()
var stories = [{"title": 'Story2', id : "56f4cf96dd2ca7275feaf7b7"},
{"title": 'Story4', id : "56f4cf96dd2ca7275feaf7ac"},
{"title": 'Story1', id : "56f4cf96dd2ca7275feaf802"},
{"title": 'Story3', id : "56f4cf96dd2ca7275feaf805"}];
var idArray = ["56f4cf96dd2ca7275feaf802",
"56f4cf96dd2ca7275feaf7b7",
"56f4cf96dd2ca7275feaf805",
"56f4cf96dd2ca7275feaf7ac"];
var ordered = stories.sort(function(a, b){
return idArray.indexOf(a.id) - idArray.indexOf(b.id);
});
ordered.forEach( element =>{ console.log(element) });
Try this
var idsToIndexes = {};
for (var i = 0; i < idArray.length; i++)
idsToIndexes[idArray[i]] = i;
stories.sort(function(a, b){return idsToIndexes[a.id] - idsToIndexes[b.id];});
本文标签: javascriptSorting Array of Objects based on Index in other arrayStack Overflow
版权声明:本文标题:javascript - Sorting Array of Objects based on Index in other array - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744198422a2594848.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论