Javascript Arrays and const

Hi all

If I have an array myNumbers = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16];

How can I rewrite the array in the form?

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

Any help would be greatly appreciated.



What do you have so far? What have you tried? Where did you get stuck?

We don’t write code for users, but we’ll happily help you troubleshoot whatever you’ve started.

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

haven’t you done that here?

in what way do you want to rewrite it?

1 Like

Hi Jeremy.

Thanks for responding.

I have taken out an old javascript program that I abandoned a few years back so I am a bit rusty on javascript. For the past few months, I have been coding in java. The array I start with is a 1 D array. I need to split it into, in this example, 4 equal parts, My research suggests that I can use array.slice to split the 1 d array up.

for example

function func() {
var article = [2, 4, 3, 7, 10, 11, 12, 15, 19];

var heading = article.slice(0, 3);
var heading2 = article.slice(3, 6);
var heading3 = article.slice(6, 9);



Ultimately if the array has been successfully split as required and copied into the const rows then I need to be able to save the 2d array as a .txt file.

I have figured out the save code but am having trouble adding the segments of the 1 D array into essentially a 2D javascript array. I know javascript does not support the declaration of 2 D arrays and its the first time I have encountered the const keyword. So I have tried to declare a 2 D array by declaring a 1 D array and then declaring further 1 D arrays inside that array, Im just not sure how to equate that to the const keyword which is used by the code that saves the array to a .txt file.

If you want me to write more of what I have tried I can do so.

Thanks again.


OK so this does the trick once the slice has succeeded

  var one0 = [1, 2, 3, 4];
        var one1 = [5, 6, 7, 8];
        var one2 = [9, 8, 7, 6];
        var one3 = [5, 4, 3, 2];

        function myFunction() {
            const rows = [
1 Like

const and let are just the updated version of var.

If you always have 16 entries, then yeah, you could just make an array that holds those slices. In Javascript, a 2D array is just an array of arrays, so

const row1 = ['a', 'b', 'c'];
const row2 = ['d', 'e', 'f'];
const array2d = [row1, row2];

you can put arrays inside of arrays, roughly speaking, in order to make an array of arrays.

Awesome, just the sort of thing I was thinking.

There are more reusable ways to do this if you don’t have fixed lengths, but if this works, it works!

Thank you Jeremy.

Actually the length of the 1D array will vary from case to case as will the split. However it will always have an even number of entries and will split into n×n sub arrays.
Its late here so I will try your solution tomorrow.
Thank you. Your help is very much appreciated.


This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.