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.
5 Answers
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];
1 Comment
Jakob
This is the most reasonable answer by far. Simple and not full of errors.
Here is the simple way to create an array collection
var createCollection = function() {
//Define a blank array
this.employee = [];
//Creating first object and push it into array
var emp1 = {
'Name': 'ABC',
'Age': 30,
'Profession': 'Software'
};
this.employee.push(emp1);
//Creating Second object and push it into array
var emp2 = {
'Name': 'CDE',
'Age': 21,
'Profession': 'Advocate'
};
this.employee.push(emp2);
//Creating Third object and push it into array
var emp3 = {
'Name': 'RTY',
'Age': 22,
'Profession': 'Teacher'
};
this.employee.push(emp3);
};
var createCollection = new createCollection();
//returns the length of the collection
alert(createCollection.employee.length);
//You can access the value from Array collection either through indexing or looping
alert(createCollection.employee[0].Name);
alert(createCollection.employee[0].Age);
alert(createCollection.employee[0].Profession);
Comments
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");
5 Comments
Jakob
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.Harish
i just gave the variety of methods anyway thanks for your suggestion.
Raynos
@Jakob this is just a matter of style choice. Not a valid reason to downvote.
Jakob
@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).Raynos
@Jakob yes
with is bad. It isn't error prone, if people don't understand that calling Array(len) creates a new Array with len elements all initialised to the literal value undefined then it's their own fault. Using new Array(len) is valid if you want that behaviour. The difference is speed is neglible compared to readability, style consistency and maintenance.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.
2 Comments
Theo
Seriously? You suggest using a library for creating arrays?
Raynos
@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.
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];
}
8 Comments
Jakob
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).Liutas
Not bad to use (for .. in .. ) if you understand there to use.
Jakob
@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.
Raynos
@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. |