function toThousand(num) {
// reduce如果没有提供第二个参数initialValue的初始值,则将使用数组中的第一个元素做为初始值
// 如果提供了initialValue,则起始索引号为0,否则从索引1起始。
return num.toString().split('').reverse().reduce((prev, next, index) => {
return ((index%3) ? next : (next + ',')) + prev;
})
}
console.log(toThousand(12345678));