-1

I'm new to javascript. So this question might not be good.

var arrQue = new Array(10);
  for (var i = 0; i < 10; i++) {
    arrQue[i] = new Array(6);
  }

This code works perfectly but I wanted to know without giving the array size, how can I make something like this (the following code doesn't work):

var arrQue = new Array();//don't know the size
  for (var i = 0; i < arrQue.length; i++) {
    arrQue[i] = new Array();//don't know the size
  }

And also the code contains two times creating new array. Is there easier or best way to do that creating multiple array?

And later I've to access like this:

arrQue[0][6] = "test";
arrQue[23][3] = "some test";

I found this method but think wrong somehow?

Object.size = function(obj) {
    var size = 0, key;
    for (key in obj) {
        if (obj.hasOwnProperty(key)) size++;
    }
    return size;
};

var arrQue = [];
var size = Object.size(arrQue);
for (var i = 0; i < size; i++) {
   arrQue[i] = [];
   var nextSize = Object.size(arrQue[i]);
}
3
  • What is the result you want? Commented Mar 19, 2014 at 9:55
  • Check the link i have added along with my answer.. Commented Mar 19, 2014 at 9:58
  • Provide me with an example input, then i can help you out. Commented Mar 19, 2014 at 10:02

3 Answers 3

4
var arrQue = [];

for (var i = 0; i < length of your inputs; i++) {
    arrQue.push(input);
}

Take a look here

Check out the Array Object Methods there.. that's all the stuff you need.

You can have arrays,arrays of objects... etc..depending upon your requirement.

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

6 Comments

[] is technically "better" than new Array().
You're saying the length of your inputs, but I don't know the size exactly.
Allright fair enough,how are you inputting? Is it through a click...? or JSON? or... how?
What you want is "Dynamically adding values into multidimensional array". stackoverflow.com/questions/10487970/…
You have kinda deviated away from the initial question,i suggest you create a new question with proper inputs and what you want as the output. I will be happy to answer it ,if i can.
|
0
var arrQue = [];
for (var i = 0; i < 10; i++) {
    arrQue.push(input);
}

Comments

0

you might be looking for the push method:

var arr = [];
arr.push(your value);  

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.