【发布时间】:2021-10-14 23:46:29
【问题描述】:
需要你的帮助。我有一个类似的数组;
const arr =
[ { item: { 0: 'asd' }, que: '1.question asdsad ... ' }
, { item: { 0: 'asd' }, que: '2.question asdsad ... ' }
, { item: { 0: 'asd', 1: 'asdsa', 2: 'asdsa', 3: 'asdsa' }, que: '3.question asdsad ... ' }
, { item: { 0: 'xx', 1: 'ssss', 2: 'ggg' }, que: 'Question asdsad ... ' }
, { item: { 0: 'asd' }, que: '4.question asdsad ... ' }
, { item: { 0: 'asd' }, que: '5.sdd asdsad ... ' }
, { item: { 0: 'bbb', 1: 'bbb' }, que: 'Dsad question asdsad ... ' }
]
如您所见,它们中的每一个都有不同的数据。我想创建像块数组这样的新数组。但它必须是;
**Maximum array length = 2
**Order must be same
**if array item length bigger than 3 or equal, it creates self array.
**if array item que does not begin with number, it create self array.
预期输出是;
const expArr =
[ [ { item: { 0: 'asd' }, que: '1.question asdsad ... ' }
, { item: { 0: 'asd' }, que: '2.question asdsad ... ' }
]
, [ { item: { 0: 'asd', 1: 'asdsa', 2: 'asdsa', 3: 'asdsa' }, que: '3.question asdsad ... ' } ]
, [ { item: { 0: 'xx', 1: 'ssss', 2: 'ggg' }, que: 'Question asdsad ... ' } ]
, [ { item: { 0: 'asd' }, que: '4.question asdsad ... ' }
, { item: { 0: 'asd' }, que: '5.sdd asdsad ... ' }
]
, [ { item: { 0: 'bbb', 1: 'bbb' }, que: 'Dsad question asdsad ... ' } ]
]
我尝试了chunk分割数组的方法。
arr.reduce((prev, next) => {
if(Object.keys(next.item).length >= 3) {
// Can be what
}
if(isNaN(next.que.split('')[0])) {
// Can be what
}
})
上面的函数仍然给我两个大小的数组,不适用规则
【问题讨论】:
标签: javascript arrays chunks