Github: count the number of elements in an array. Reduce ()

const countOccurrences = (arr) = > arr.reduce((prev, cur) = > {
  if (cur in prev) {
    ++prev[cur]
  } else {
    prev[cur] = 1
  }
  return prev
}, {})
Copy the code

The great God is written:

const countOccurrences = (arr) = > arr.reduce((prev, curr) = > ((prev[curr] = ++prev[curr] || 1), prev), {});
Copy the code

A brief introduction of | | or logical operators

  • Returns true if any of the arguments involved in the operation are true, false otherwise;

  • console.log(true || false) // true
    console.log(false || true) // true
    console.log(true || true) // true
    console.log(false || false) // false
    Copy the code
  • So if a given multiple parameters, such as 1 | | | 2 | 3 this kind of situation? Directly show code

  • console.log(1 || 2 || 3) / / 1
    console.log(66 || 2 || 3) / / 66
    console.log(1 || 0 || 3) / / 1
    console.log(null || null || 3) / / 3
    
    console.log(null || undefined || 0) / / 0
    console.log(null || 0 || undefined) // undefined
    Copy the code
  • Find that if the result is true, then return the first true value; If the result is false, the last value is returned


Looking back code (prev [curr] = + + prev [curr] | | 1), the now is easy to understand, but let me confused is main, the reduce () method of prev parameter is the last time the callback return values, but this code doesn’t see return, this is how to return a responsibility? Is it a comma? In my mind, I did dimly remember that there was a comma operator, and when I looked it up, it was right.

Comma operator

Evaluates each of its operands from left to right and returns the value of the last operand

Look directly at the code:

let a = 10
let b = 12
let c = 15
let d
d = (a, b++, c++, 100)
console.log(a, b, c, d) // 10 13 16 100
Copy the code

Now I have a general understanding of it. Suddenly I thought of this question in the written test when I went out for an internship interview in my senior year. Now I suddenly realized:

var i = j = 0, k = 0;
for(i, j; i < 10, j < 6; i++, j++) {
   k = i + j;
}
console.log(k); / / 10
Copy the code

At the beginning, I naively thought that the condition of the end of the cycle was I <10, and the small term in j<6, that is, j>=6, broke out of the cycle, so it was correct and the result was 10. The comma operator returns the value of the last operand, as shown in the following example:

var i = j = 0, k = 0;
for(i, j; i < 6, j < 10; i++, j++) {
   k = i + j;
}
console.log(k); 
Copy the code

If I had followed my earlier thought, the order of transposition would have made no difference, but the result is 18, which is a good indication that the comma operator is really doing the work, k = 9 + 9 =18, and when j=10, the loop is broken.

conclusion

Looking back again, both my writing method and daishen’s writing method have already returned prev, but the writing method of the latter seems to be more concise. Because I am untalented and uneducated, I did not react at the first time, and I also gained another knowledge point today.