【发布时间】:2020-11-04 20:55:24
【问题描述】:
我有这个数组["Brad", "came", "to", "dinner", "with", "us"]
Brad came to dinner with us (27 chars > 16) 所以我需要将其拆分为 2 个字符串:
Brad came to
dinner with us
单词不能切片
let inpt=["Brad", "came", "to", "dinner", "with", "us"]
op=[]
for(i of inpt) if (op.join('').length+i.length <16) {op.push(i)} else break
console.log(op.join(' '))
这将返回我的第一部分,但如果我的输入数组(字符串)长于 16+16+16...,我如何获得第二部分和其他部分......
【问题讨论】:
-
嗨,也许一旦达到限制就开始追加到新列表?
标签: javascript arrays reduce