0

I try to create a 4-dimensional array. I fill it dynamically and use content of it in another function. But the content is empty. Is there error below code?

    var datas = []; // day number of a week
    for(var i = 0; i < 7; i++) {
            var size = 24*60/timeInterval;
            datas[i] = [];

            for(var j = 0; j < size; j++) {
                var size2 = allCoords.length / 2;
                datas[i][j] = [];

                for(var k = 0; k < size2; k++) {
                    datas[i][j][k] = [];
                }
            }
        }

I test below example :

function foo1()
{
    datas[0][0][0].push(10);
}

function foo2()
 {

     document.getElementByID('result').innerHTML = datas[0][0][0];
 }

I see only ,,,,,,,.

8
  • How are you calling foo1 and foo2, and how often? Commented Feb 20, 2015 at 12:48
  • Why does foo1 only push to datas[0][0][0], do you need that four-dimensional array at all? Please show us your actual code that fills it with content. Commented Feb 20, 2015 at 12:49
  • this works for me Commented Feb 20, 2015 at 12:51
  • You're better off using console.log() than document.getElementByID('result').innerHTML = .... You're just seeing a string representation (of an empty array). The console will be more informative. Commented Feb 20, 2015 at 12:51
  • what are the values of timeInterval and allCoords? If they are zero that may be your problem Commented Feb 20, 2015 at 12:51

1 Answer 1

1

I think the principal problem is that you're getting the element where you want to show your result badly using getElementByID instead of getElementById. Also make sure that your element has innerHTML property to write the result, or alternatively use value.

I write the follow example using <textArea id="result"></textArea> and generating a button which calls foo1();foo2(); onClick an it works for me.

In the sample I use an random value for timeInterval and allCoords.length.

Note also that you want a 4-dimensional array however you're creating a 3-dimensional.

var timeInterval = 60;
var allCoords = { length : 1};
var datas = []; // day number of a week
    for(var i = 0; i < 7; i++) {
            var size = 24*60/timeInterval;
            datas[i] = [];

            for(var j = 0; j < size; j++) {
                var size2 = allCoords.length / 2;
                datas[i][j] = [];

                for(var k = 0; k < size2; k++) {
                    datas[i][j][k] = [];
                }
            }
        }


function foo1()
{
    datas[0][0][0].push(10);
}

function foo2()
{
   document.getElementById('result').value = datas[0][0][0];
}
<textArea id="result"></textArea>
<input type="button" value="foo" onclick="foo1();foo2();"/>

Hope this helps,

Sign up to request clarification or add additional context in comments.

4 Comments

Sorry, I don't see anything when I write on text field and press foo.
why you write on text area? simply press foo which writes the data[0][0][0] on the textArea. Which it's your purpouse, isn't it?
@cyclecyletic anyway I change the property innerHTML for value so the characters in the textArea will be overwrite :)
ok. :) the code is right, I should look at other part of my code. thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.