Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

443
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda