Work with arrays. The forEach method and its working principle?

const arr = [1, 3, 4, 1, 1, 3, 4, 5];
const result = {};

arr.forEach((a) => {
 if (result[a] !== undefined) {
 } else {
 result[a] = 1;

for (let key in result) {
 console.log('number %d == %d time(s)', +key, result[key]);

Please explain why repeating numbers is not equal to undefined?
April 3rd 20 at 18:45
1 answer
April 3rd 20 at 18:47
forEach() calls a function with each element of an array. In this example, the function will be called(1), function(3) function(4), ... function(5)

the result is an object where the properties are met by the numbers (as strings).

The figure is not met, such properties result no, and, for example, result[1] is initially undefined. And in this case it creates this property with a value of 1.

But if such a property (with value-counter = 1 or no longer) found, this value is not undefined, and it will be increased by 1.

And sochitaetsya the number of times each of the values.

Find more questions by tags JavaScript