necesito tu ayuda. Tengo una matriz como;
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 ... ' } ] Como ves cada uno de ellos tiene datos diferentes. Quiero crear una nueva matriz como una matriz de fragmentos. Pero tiene que ser;
**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.El resultado esperado es;
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 ... ' } ] ]Traté de fragmentar el método de dividir la matriz.
arr.reduce((prev, next) => { if(Object.keys(next.item).length >= 3) { // Can be what } if(isNaN(next.que.split('')[0])) { // Can be what } })La función anterior todavía me da una matriz de dos tamaños, no se aplica la regla
Básicamente, puede hacer un bucle en cada objeto y, dentro de una instrucción if , averiguar si el objeto se agrega a una nueva matriz o si se agrega a la matriz existente. Es más fácil de ver en este fragmento:
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 ... ' } ]; let arNew = []; let arTmp = []; let sCurItemO = ''; for(const obj of arr){ if(obj.item['0'] != sCurItemO || arTmp.length == 2 || Object.keys(obj.item).length > 2 || isNaN(obj.que.substring(0, 1))){ if(arTmp.length) arNew.push(arTmp) arTmp = [obj] } else arTmp.push(obj); sCurItemO = obj.item['0']; } arNew.push(arTmp); console.log(arNew)eso lo veo asi...
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 ... ' } ] const maxSize = 2 const itemMax = 3 const result = [] let lastArr = null for (let row of arr) { let len = Object.keys(row.item).length if ((len >= itemMax) || isNaN(row.que.charAt(0))) { result.push([row]) lastArr = null } else { if (!lastArr || lastArr.length >= maxSize) { lastArr = new Array() result.push(lastArr) } lastArr.push(row) } } console.log( result ) .as-console-wrapper { max-height: 100% !important; top: 0 }