0

I use an api which returns an object 'results'. The 'results' object holds other objects which have type article or detail.

Each one of the the result (article or detail) contain their own object results with different pages of that type.

What i want to do: store the results in an array 'pages' where i order them by all the articles first then all the details

I want to sort them in the array because I need to display the articles first then the detail pages in 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);
        });
    }
});

This was my attempt with Lodash find but in the frontend i still have results where the details are displayed after the articles.

Note: The api result can be like this: {detail}, {article} but also like this {detail}, {article}, {detail}, {article}

Sample data:

results: [
    {
        type: 'article',
        results: {
            {
                title: '',
                body: '',
                ...
            },
            ...
        }
    },
    {
        type: 'detail',
        results: {
            {
                title: '',
                body: '',
                ...
            },
            ...
        }
    },
    ...
]
7
  • Could you provide a minimal sample of the data (sorted as expected) ? Commented Sep 9, 2016 at 9:45
  • @procrastinator I added a sample of how the results object looks like Commented Sep 9, 2016 at 9:54
  • It seems to me that find is useless in this case (your predicate does not return any value, see lodash.com/docs/4.15.0#find). Commented Sep 9, 2016 at 9:54
  • @procrastinator yes i first tried with forEach but I had problems with the sorting there as well, I thought that with find i could first look for the articles and push them first. Commented Sep 9, 2016 at 9:57
  • "What i want to do: store the results in an array 'pages' where i order them by all the articles first then all the details". Then, you want to change [ [ d, d, d ], [ a, a, a ], [ d, d, d ] ] to [ a, a, a, d, d, d, d, d, d ], right ? Commented Sep 9, 2016 at 10:04

1 Answer 1

1

After a long dicussion :

var sorted;
var articles = [];
var details = [];

var source = [{
  app: "article",
  results: [
    { title: "a" },
    { title: "b" },
    { title: "c" }
  ]
}, {
  app: "detail",
  results: [
    { title: "d" },
    { title: "e" },
    { title: "f" }
  ]
}, {
  app: "article",
  results: [
    { title: "g" },
    { title: "h" },
    { title: "i" }
  ]
}];

for (var i = 0; i < source.length; i++) {
  switch (source[i].app) {
    case "article": articles = articles.concat(source[i].results); break;
    case "detail": details = details.concat(source[i].results); break;
  }
}

sorted = articles.concat(details);
console.log("articles =", JSON.stringify(articles));
console.log("details =", JSON.stringify(details));
console.log("sorted =", JSON.stringify(sorted));
console.log("source =", JSON.stringify(source));

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.