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
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.