Array intersection

Find the common elements in an array with no duplicates ... works with any type of element in the array

by marcfawzi

JavaScript

Object.prototype.equals = function(obj) {
    return JSON.stringify(this) === JSON.stringify(obj) ;
}

var arr1 = 	[1, [2], 3, {a: 4}, 5, 6, 6, [7]]

var arr2 = [[2], {a: 4}, 10, 10, 11, 7]

var common = []

arr1.forEach(function(d) {

	var found = arr2.some(function(num) { return num.equals(d)})
	
	if (found && !common.some(function(num) { return num.equals(d)})) common.push(d)
})

console.log(common)