1

I'm trying to pass a two dimensional array into a JavaScript function but I only get the first element of the two dimensional array. Below is an excerpt of my code:

header = [['Objective','summary'],['Status','txtHealth']];

...


function setTableHeader(data){
    console.log(data);
    var table = document.getElementById('tblData').tHead.insertRow(0);

    for (var i = 0; i < header.length; i++)
        table.insertCell(i).innerHTML = data[i];

};


/*calling the function*/
setTableHeader.apply(this,header);

The console log shows only ['Objective','summary'], is it because the function gets passed only the pointer to the memory location of the first memory block of the array?

I'm new to webdev and I'm also curious if I should be using global variables whenever I can rather than local variables?

2
  • 2
    Why are you using apply? Try just calilng the function setTableHeader(header) Commented Sep 23, 2013 at 0:26
  • 1
    "curious if I should be using global variables whenever I can rather than local variables", try to keep the scope of variables as tight as possible, so local variables should be preferred over global Commented Sep 23, 2013 at 0:27

3 Answers 3

3

First issue is you are using apply which takes argument as array So you should do.

setTableHeader.apply(this,[header]);

otherwise your header array's each row will go in as its own argument to the function. So your data will be just ['Objective','summary']

But you can just invoke the function as is here:

  setTableHeader(header);

Also if you have header defined as global you don't need to pass the data through to the function as argument. Hoewever your for loop checks for the length of header array instead of the passed in data length. Which mismatches here due to the apply issue.

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

3 Comments

I guess it was really late yesterday when I tried just passing header because it wasn't working. So I Googled and found the apply method. It works like a charm. Thanks!
@codeBarer You are welcome... But why do you need to use apply here? ALso make it a good practice to use var while defining variables and try not to pollute globals as much..
I googled it yesterday because when I was passing header it was not doing anything. So I thought that perhaps that might be the proper way of passing array in javascript. I'm new to javascript.
1

The for loop should be looping until data.length, not header.length.

Comments

0

Function arguments in javascript are passed either as value or as reference.

Primitive types, like int, string are passed as value.

Object types, like array, object are passed as reference.

In your case, apply is the devil here. apply takes an array of arguments and apply them to the function as positional arguments, which means the array of header got expanded. So if you want to get your header right, do it like this:

setTableHeader.apply(this,[header]);

Comments

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.