When using an API, I receive an object called 'results' which contains other objects of type article or detail.
Each result (article or detail) includes its own results object with different pages of that type.
My goal is to organize these results in an array named 'pages' where all the articles come first followed by all the details.
The reason for sorting them in the array is because I need to prioritize displaying the articles before the detail pages on the frontend.
var pages = [];
_.find(results, function(r) {
if(r.type === 'article') {
_.forEach(r.results, function(p) {
p.type = 'article';
pages.push(r);
});
} else if(r.app === 'detail') {
_.forEach(r.results, function(p) {
p.type = 'detail';
pages.push(p);
});
}
});
I attempted to achieve this using Lodash find, but I still encounter issues with the display order on the frontend, where the details sometimes appear after the articles.
It's important to note that the API can return combinations like {detail}, {article} or even {detail}, {article}, {detail}, {article}.
Sample data:
results: [
{
type: 'article',
results: {
{
title: '',
body: '',
...
},
...
}
},
{
type: 'detail',
results: {
{
title: '',
body: '',
...
},
...
}
},
...
]