const countBy = (arr, fn) =>
arr.map(typeof fn === 'function' ? fn : val => val[fn]).reduce((acc, val, i) => {
acc[val] = (acc[val] || 0) + 1; return acc;
}, {});
countBy([6.1, 4.2, 6.3], Math.floor); // {4: 1, 6: 2}countBy(['one', 'two', 'three'], 'length'); // {3: 2, 5: 1}我的疑问是map里面是怎么运行,arr.map() 主要是这个三元表达式结果是应该是进入fn,而不是后面的reduce求大佬着重解释下这个
红糖糍粑
相关分类