Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

435
Vistas
How to save a Javascript array as a .txt file

Good day all.

the following code quite easily saves the "const rows" array as a .txt file

function exportAdjacency() {

            const rows = [
                [-1, 183, 294, -1],
                [183, -1, 171, 306],
                [294, 171, -1, 187],
                [-1, 306, 187, -1]
            ];


            const csvContent = `data:text/csv;charset=utf-8,${rows
                .map((e) => e.join(","))
                .join("\n")}`;
            const encodedUri = encodeURI(csvContent);
            const link = document.createElement("a");
            link.setAttribute("href", encodedUri);
            link.setAttribute("download", "data.txt");
            document.body.appendChild(link);
            link.click()
        }

However, in my case, my array is not formatted as in the code above. In fact, I have a 1 D array of, in this case, 16 numbers, which I need to feed into the above code.

I have tried to segment the array using array.slice as below


function func() {

    var article = [1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15,16];

    var heading = article.slice(0, 4);
    var heading2 = article.slice(4, 8);
    var heading3 = article.slice(8, 12);
    var heading4 = article.slice(12, 16);
        
    alert(heading)
    alert(heading2)
    alert(heading3)
    alert(heading4)
            
}

The alerts just confirm that the segmentation was successful.

My question is, how do I segment the 1 D array and feed its contents into the

"const rows" of the first code example?

I have tried changing the first code snippet to rather accept something like

"array rows" but this doesn't work.

Now in this example, the 1 D array has 16 elements. In the general case, it could have any number of elements.

Any help would be greatly appreciated.

Thanks

Paul

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

The slice() method returns selected elements in an array, as a new array.

The slice() method does not change the original array.

const variable cannot be reassign, use let, to assign segment to rows (let), And use splice() method overwrites the original array.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda