使用 .reduce
后如何找到数组中的字符总数(包括空格和逗号)?
例如。
假设我有一个数组,当使用 .reduce((prev, curr) => [prev, ', ', curr])
时,会导致
array: item1, item2, item3
当我找到字符数时,它是 19(没有“array:”)。
最佳答案
我建议您使用join
方法而不是reduce
。对于您的情况,
let array = ['item1', 'item2', ' item3 '];
const strLength = array.join().length;
关于javascript - 求减少后数组中的字符总数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60661629/