es6 print word and count from array code example

Example 1: word count algorithm javascript

function getWordCount() {  let map = {};for (let i = 0; i < array.length; i++) {    let item = array[i];    map[item] = (map[item] + 1) || 1;  }  return map;}getWordCount(array); // { apple: 3, orange: 2 }

Example 2: javascript Count the occurrences of a value in an array

const countOccurrences = (arr, val) => arr.reduce((a, v) => (v === val ? a + 1 : a), 0);

// Examples
countOccurrences([2, 1, 3, 3, 2, 3], 2);                // 2
countOccurrences(['a', 'b', 'a', 'c', 'a', 'b'], 'a');  // 3