Hey guys, I'm new to JS and basically learn new stuff every day, I just learn things like dynamically updating the array and stuff, anyway I usually try to pick up clean pieces of JS network and debug them until I understand what does it really mean. so here is the snippet I found today:
var array1 = [ { tagId: 1, tagName: 'tag 1' }, { tagId: 2, tagName: 'tag 2' }, { tagId: 3, tagName: 'tag 3' }, { tagId: 4, tagName: 'tag 4' } ]; var array2 = [ { tagId: 1, tagName: 'tag 1' }, { tagId: 2, tagName: 'tag 2' }, { tagId: 8, tagName: 'tag 8' } ]; var array3 = [ { tagId: 1, tagName: 'tag 1' }, { tagId: 0, tagName: 'tag 0' }, { tagId: 9, tagName: 'tag 3' }, { tagId: 12, tagName: 'tag 12' }, { tagId: 13, tagName: 'tag 3' }, { tagId: 14, tagName: 'tag 2' }, { tagId: 6, tagName: 'tag 6' }, ]; var a4 = common(array1, array2, array3) console.log(a4); function common( /*…*/ ) { var a = arguments; res = a[0] for (var i = 1; i < a.length; i++) { res = res.filter(function (el) { return a[i].filter(function (el2) { return el2.tagId === el.tagId }).length }) } return res }
basically a fragment goes through arrays with object literals in them and filters them out. Now, my question. being new to JS, I came across an interesting example that many of the JS code uses the return in quick succession, or rather, one by one, they are often nested, this is a very interesting case for me, because I usually assume that one expression of return should be all necessary.
anyway, back to my example and what is my difficulty, if you see the code inside the common function , you will see the code below:
res = res.filter(function (el) { return a[i].filter(function (el2) { return el2.tagId === el.tagId }).length })
I really don't understand the double refund instructions. I understand return el2.tagId === el.tagId returns true or false and returns
a[i].filter(function (el2) { return el2.tagId === el.tagId }).length
returns the length, which I think should be an integer, but what is the order in which the return is performed? who return el2.tagId === el.tagId returns to ??,
I am always perplexed when I see the expression of double return, can someone explain this to me, please?
jsfiddle here .
EDIT :: try and come up with your answer a bit.
Thanks.
Alex Z