Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I want to create an array from user input. I dont know the details of input, so basically I just need to define, initialize and populate the array with input data, so that I can do further actions later.

example array could look something like this:

var input = ["1" : ["0": "Apple",
                    "1": "Orange"],
             "2" : ["0", "Cat",
                    "1", "Dog"],
             "4" : ["0", "Coffee"]
        ];

Now, I dont know how to define, initialize and populate it, as this is not working:

    var input = [[]];
    $('.input').each(function(index, element) {
        var element_id = this.id;
        var element_value = $.trim(this.value);
        input[element_id][index] = element_value;
    });

Any javascript experts who could help?

share|improve this question
2  
[[]] does not create a matrix but a simple array containing another array in first index. – Virus721 Aug 1 '13 at 13:24
up vote 3 down vote accepted

Change your code to

var input = {};
$('.input').each(function(index, element) {
    var element_id = this.id;
    var element_value = $.trim(this.value);
    (input[element_id]||(input[element_id]={}))[index] = element_value;
});

You'll get something like

    {"1" : {"0": "Apple",
                "1": "Orange"},
         "2" : {"0", "Cat",
                "1", "Dog"},
         "4" : {"0", "Coffee"}
    };

which isn't arrays inside arrays but maps inside maps, which seems to be more indicated for you.

Now, suppose you really want arrays, then instead of strings as keys you need to have integers. Then you may do

var input = [];
$('.input').each(function(index, element) {
    var element_id = parseInt(this.id,10);
    var element_value = parseInt($.trim(this.value),10);
    (input[element_id]||(input[element_id]=[]))[index] = element_value;
});
share|improve this answer
    
Great, Seems like it's working now. Thx! – art2 Aug 1 '13 at 13:38
    
Question further, later I need to grab the last numeric index of the array, so does it matter if its 'map' or 'array'? Because I read somewhere that if its an object then it might not get the 'highest' numeric but any random object index. – art2 Aug 1 '13 at 13:41
1  
If the order is important to you, and if your keys are fitting (I mean they're more like 1, 4, 5, 8, 9 than like -45, 'hi!', 93, '+Infinity'), then you should use an array. Of course you can have arrays inside a map or maps inside an array. – Denys Séguret Aug 1 '13 at 13:43
    
Yes exactly, the order is important as the highest key is which I want to know. Thank you again! – art2 Aug 1 '13 at 13:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.