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 combinations 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"}]