Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

Is it possible to create an array collection in JavaScript? If so, how can I do that? If I have some data : 1- 5 for example.

share|improve this question

4 Answers 4

up vote -3 down vote accepted

You can create object:

var obj = {
   my1 : 'data',
   my2 : 'other'
};

Or

  var array = ['data',  'other'];

Access all data you can

for(var key in array) {
 item = array[key];
}

for(var key in obj) {
  item = obj[key];
}
share|improve this answer
2  
Gah, NEVER use for (.. in ..) to iterate over an array. You should really read more about it, for your own sake! Instead, use: for ( var i=0; i<array.length; i++) { item = array[i]; } or even better, forEach from JavaScript 1.6 (or its substitute from jQuery, prototype, underscore or any other jslib under the sun). – Jakob Mar 28 '11 at 8:03
2  
@Jakob If your going to correct someone at least get it right. The native forEach and the jQuery, prototype or underscore abstractions are NOT faster. benchmark. – Raynos Mar 28 '11 at 9:37
    
Not bad to use (for .. in .. ) if you understand there to use. – Liutas Mar 28 '11 at 11:54
    
@Raynos: never said it was faster. The for-loop with the index sure is faster than the other methods. @Liutas, for (.. in ..) should not be avoided altogether, of course you can use it for objects just like you did. BUT, for arrays you should never do it. Let me make you an example. – Jakob Mar 28 '11 at 12:59
    
@Jakob woh, I seem to have misread better as faster. I agree don't use for in. Either use an each abstraction and treat it as an enumerator or use a for loop and treat it as procedural iterator. Personally I always use for if I want to treat it as an Array. I use an each abstraction when I treat an array as a Set. – Raynos Mar 28 '11 at 13:04

I actually do not quite understand what you want to do, but you can create a Javascript array containing the numbers 1 to 5 like this:

var myArray = [1, 2, 3, 4, 5];
share|improve this answer
    
This is the most reasonable answer by far. Simple and not full of errors. – Jakob Mar 28 '11 at 8:07

A-Z of javascript Arrays

check this link

If you want to populate an array

var tmpArray = new Array (4);
tmpArray [0] = "a";
tmpArray [1] = "b";
tmpArray [2] = "c";
tmpArray [3] = "d";

Or

var tmpArray= new Array ("a", "b", "c", "d"); 
share|improve this answer
    
You should always prefer the literal-syntax over the last example. And the first example is using the very error-prone single parameter constructor, which is never a good idea. Using the push method is a lot cleaner and will barely have a performance impact. – Jakob Mar 28 '11 at 8:02
    
i just gave the variety of methods anyway thanks for your suggestion. – Harish Mar 28 '11 at 8:23
2  
@Jakob this is just a matter of style choice. Not a valid reason to downvote. – Raynos Mar 28 '11 at 9:38
    
@Raynos: The first one is error prone, it's like saying that using with is a matter of choice; it should be avoided if possible. I would agree that the second example is a matter of choice though, you're right there. It's still very unidiomatic (if that's a word) and also slower, since it forces the interpreter to examine the scope chain to see if Array has been defined. Not that I think such a tiny thing matter, but my point is that it's not ONLY a style choice (even if 95% so). – Jakob Mar 28 '11 at 12:55
    
And by the way, I didn't downvote it; I just commented. – Jakob Mar 28 '11 at 12:56

If you want a more complex collection of data you can use the data.js abstraction library.

If your more specific in what you want I can show you an example.

share|improve this answer
    
Seriously? You suggest using a library for creating arrays? – Theo Mar 28 '11 at 8:44
    
@Theo no. I'm suggesting a library for creating collections. I really depends on what he is doing with the data. Did the SO down-voting police make a bombing strike here? The question was vague, maybe he wanted a complex data structure. – Raynos Mar 28 '11 at 9:32

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.