The point is I'm not an expert with JS so excuse me if my code is a big mistake, in fact I'm just training to learn making a huge "Frankenstein" from a research around the web.
I want to take two set or lists of values input by the user and compare them looking for matches with js and jquery.
First, I take the values from two groups of inputs (diferenced by class) with .map(), this to have the values to create the arrays.
var x = $(".row_a").map(function() {
return this.value;
}).get();
var y = $(".row_b").map(function() {
return this.value;
}).get();
Then I'm traying to create one variable that contain the two arryas separatly (here Is when think I have the problem) if I "hardcode" the arrays the next part of the script works as expected, but if I use the the first two variables made above, the script just crash.
var arrays = [
[x],
[y]
];
In the third part (I really don't understand this part of the code in deep) I run the script that compare the two arrys on the second variable and then append a paragraph with the result.
var result = arrays.shift().reduce(function(res, v) {
if (res.indexOf(v) === -1 && arrays.every(function(a) {
return a.indexOf(v) !== -1;
})) res.push(v);
return res;
}, []);
$(".match").append("<div>Numbers " + result + " match in both lists</div>");
Somebody can help me to undersnatd whats wrong or giveme a clue or link that can help?
curious: if I use the same variable twice in the variable that contain the arrays, the script works and finds four matches (I think is right becouse compare the same array)
--
EDITED:
Thanks to @KarlReid and @Potorr. I didn't know the intersection concept in javascript, so now I know a better way to achive the result. I'll read more about it to try understand the answer in deep.
Thanks to @Alexandru for letting me know the sintax error, it'll be very basic and usefull henceforth.
Final Result: DEMO
(I'll be edit the tittle of the post to try improve the search for others with the same question in the future.)