I was looking for a solution to finding multiple values in an array, and found this:
function find_duplicate_in_array(arra1) {
var object = {};
var result = [];
arra1.forEach(function(item) {
if (!object[item])
object[item] = 0;
object[item] += 1;
})
for (var prop in object) {
if (object[prop] >= 2) {
result.push(prop);
}
}
return result;
}
console.log(find_duplicate_in_array([1, 2, -2, 4, 5, 4, 7, 8, 7, 7, 71, 3, 6]));
I don't understand what is happening. Specifically this:
object[item] = 0;
object[item] +=1;
So... for each element in the array, if the element is not in temporary object add element at index 0, and then +1?.
What's going on? Would someone please explain line by line. I am new to JavaScript.