0

I am a newbie in Node.js and still learning the basic syntax

I was wondering if we want to use two different JSON object and then compare their values before giving the output then how do we use forEach in a nested manner like normal for?

var count = 0;
app.locals.author.forEach(function(item)
{
    app.locals.post.forEach(function(item1){

        if(item.id == item1.userId)
        {
            count++;
        }
    });
    res.send('Author = ' + item.name + " No of Posts done = " + count+"\n");
    count = 0;
});
White Hat
  • 3
  • 3
  • What do you mean by "one result"? Only one author, only one post? What's it supposed to look like? Also, check out https://stackoverflow.com/questions/5829007/looping-through-json-with-node-js . – LGT Dec 07 '17 at 09:20
  • one result means to get a single output that will contain the fields from both the JSON objects – White Hat Dec 07 '17 at 09:30

1 Answers1

0

Looks like you're just after a filter

item.posts = app.locals.post.filter(x => x.userId === item.id)
James
  • 80,725
  • 18
  • 167
  • 237