猿问

reduce统计个数问题

在nodeschool上看到这个问题

function countWords(arr) {  return arr.reduce(function(countMap, word) {
    countMap[word] = ++countMap[word] || 1 // increment or initialize to 1
    return countMap
  }, {}) // second argument to reduce initialises countMap to {}}module.exports = countWords

传入数据

var inputWords = ['Apple', 'Banana', 'Apple', 'Durian', 'Durian', 'Durian']

得到的结果

{    //   Apple: 2,
    //   Banana: 1,
    //   Durian: 3
    // }

实现这个过程的js原理是什么


开满天机
浏览 640回答 1
1回答
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答