I came across a question which asked us to
combine arrays
var array1 = [1, 2, 3]; var array2 = [2, 30, 1];
and then remove all duplicate items so expected output was
[1, 2, 3, 30]
in the comment section there was this solution which I'm unable to understand:
var array1 = [1, 2, 3];
var array2 = [2, 30, 1];
function concatArrays(array1, array2){
var concated = array1.concat(array2);
var solution = concated.filter(function(element,Index,self){
return Index== self.indexOf(element);
});
console.log(solution);
}
concatArrays(array1,array2);
So I want to understand this line
var solution = concated.filter(function(element,Index,self){
return Index == self.indexOf(element);
});
I know what the filter method does, but then in the return part I'm unable to understand his code.
This is what I think the return part does:
Compare index of element with the index of current element. If it's equal then return else don't do anything.
I don't know if my interpretation is right or wrong.
Indexin this code stinks; it's no class.