Necesito atravesar una matriz bidimensional en zigzag y elegir los elementos en el camino:
De:
[['๐','๐','๐','๐'], ['๐บ','๐บ','๐ฉ','๐ด'], ['๐','๐ฆ','๐','๐'], ['๐','๐น','๐บ','๐']]A:
['๐','๐บ','๐','๐','๐บ','๐','๐','๐ฆ','๐ฉ','๐','๐ด','๐','๐น','๐บ','๐','๐']Mi enfoque fue usar un ciclo for, verificar cada รญndice de la primera matriz y compararlo con el รญndice de la siguiente matriz y luego, si ese nรบmero es mรกs grande en uno, empujarlo a la nueva matriz unidimensional.
ยฟCuรกl es el mejor enfoque para resolver esto? ยฟTiene algunos recursos para aprender mรกs sobre este patrรณn?
Tengo entendido que desea transformar una matriz n ร n como:
[ ['๐', '๐ฏ', '๐ป', '๐'] , ['๐', '๐ฝ', '๐ฅ', '๐'] , ['๐', '๐ฅ', '๐ฃ', '๐ฅฆ'] , ['๐ฎ', '๐งบ', '๐', '๐ฆ'] ]dentro:
['๐','๐','๐ฏ','๐ป','๐ฝ','๐','๐ฎ','๐ฅ','๐ฅ','๐','๐','๐ฃ','๐งบ','๐','๐ฅฆ','๐ฆ']Transformemos la matriz original en una "matriz de posiciones" e intentemos imaginar el "zigzag":
[ [[0,0], [0,1], [0,2], [0,3]] // โ โ โ โ , [[1,0], [1,1], [1,2], [1,3]] // โ โ โ โ , [[2,0], [2,1], [2,2], [2,3]] // โ โ โ โ , [[3,0], [3,1], [3,2], [3,3]] // โ โ โ โ ]Si nos enfocamos en los bordes, podemos comenzar a trabajar en un patrรณn:
[ [0,0] , [1,0], /* โฆ */ [0,1] , [2,0], /* โฆ */ [0,2] , [3,0], /* โฆ */ [0,3] , [3,1], /* โฆ */ [1,3] , [3,2], /* โฆ */ [2,3] , [3,3] ] Ahora necesitamos resolver todo el [x,y] entre cada borde y atravesar cada borde en direcciรณn opuesta:
const inp1 = zigzag([ ['๐', '๐ฏ', '๐ป', '๐'] , ['๐', '๐ฝ', '๐ฅ', '๐'] , ['๐', 'โ๏ธ', '๐ฃ', '๐ฅฆ'] , ['๐ฎ', '๐งบ', '๐', '๐ฆ'] ]); const inp2 = zigzag([ ['๐', '๐ฏ', '๐ป'] , ['๐', '๐ฝ', '๐ฅ'] , ['๐', 'โ๏ธ', '๐ฃ'] ]); const inp3 = zigzag([ ['๐', '๐ฏ'] , ['๐', '๐ฝ'] ]); const inp4 = zigzag([ ['๐'] ]); console.log(` [${String(inp1)}] [${String(inp2)}] [${String(inp3)}] [${String(inp4)}] `); <script> const zigzag = inp => { const m = inp.length - 1; const edges = []; for (let x = 0; x <= m; x++) edges.push([x, 0]); for (let x = 1; x <= m; x++) edges.push([m, x]); return edges.flatMap(([x, y], i) => { const path = [[x, y]]; for (let a = x, b = y; a != y && b != x;) path.push([--a, ++b]); return (i % 2 ? path : path.reverse()).map(([x, y]) => inp[x][y]); }); } </script>RESPUESTA ANTIGUA:
puede usar el mรฉtodo .flat() para la matriz de javascript . Array.flat()
let array = [ [1, 3, 4, 10], [2, 5, 9, 11], [6, 8, 12, 15], [7, 13, 14, 16], ] const flatArray = array.flat() flatArray.sort((a,b)=>ab) console.log(flatArray)RESPUESTA DE ACTUALIZACIรN: despuรฉs de la salida de actualizaciรณn de la pregunta
const items = [ [1, 3, 4, 10], [2, 5, 9, 11], [6, 8, 12, 15], [7, 13, 14, 16], ]; /*const items = [ [๐ , ๐ , ๐ , ๐ ], [๐บ , ๐บ , ๐ฉ , ๐ด ], [๐ , ๐ช , ๐ , ๐ ], [๐ , ๐น , ๐บ , ๐ ], ]*/ function zigZag(arr) { let array = [] const itemCounts = arr.reduce((pre, cur)=> pre+cur.length,0) for(let i=0; i<itemCounts; i+=1){ let round = [] for(let j=0; j<arr.length; j+=1){ if(arr[j].length){ round.push({ value: arr[j][0], row:j }) } } const minValue = Math.min(...round.map(item=>item.value)) const target = round.find(item=>item.value == minValue) array.push(arr[target.row].shift()) } return array; }; console.log(zigZag(items))RESPUESTA ACTUALIZADA
Esta funciรณn fusionarรก matrices en forma de zigzag.
Aquรญ he mostrado un ejemplo con 2 matrices con diferentes valores de tipo de datos.
function zigZag(array) { let arrayLength = array.length; let arrayItemLength = array[0].length; let result = []; let flag = true; for(let i = 0; i < (arrayLength + (arrayLength / 2) + 1) ; i++) { if(i < arrayItemLength) { let length = (i + 1); let ii = i; for(let j = 0; j < length; j++) { if(flag == true) result.push(array[j][ii]); else result.push(array[ii][j]); ii-=1; } }else { let ii = (i + 1) - arrayItemLength; for(let j = arrayItemLength - 1; j > i - arrayItemLength; j--) { if(flag == true) result.push(array[ii][j]); else result.push(array[j][ii]); ii+=1; } } if(flag == true) flag = false; else flag = true; } return result; } let array = [ ["๐" , "๐" , "๐" , "๐" ], ["๐บ" , "๐บ" , "๐ฉ" , "๐ด" ], ["๐" , "๐ช" , "๐" , "๐" ], ["๐" , "๐น" , "๐บ" , "๐" ], ]; let array_1 = [ [1, 3, 4, 10], [2, 5, 9, 11], [6, 8, 12, 15], [7, 13, 14, 16], ]; console.log(zigZag(array)); // icons console.log(zigZag(array_1)); // numbersRESPUESTA ANTIGUA
Prueba esto, creo que esto es lo que quieres hacer.
let array = [ [1, 3, 4, 10], [2, 5, 9, 11], [6, 8, 12, 15], [7, 13, 14, 16], ]; function mergeArray(array) { let merged = array.reduce((item, total) => [...total, ...item], []); return merged.sort((a, b) => a - b); } let result = mergeArray(array); console.log(result)