0

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 5

5

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];
Sign up to request clarification or add additional context in comments.

1 Comment

This is the most reasonable answer by far. Simple and not full of errors.
1

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

0

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

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.
i just gave the variety of methods anyway thanks for your suggestion.
@Jakob this is just a matter of style choice. Not a valid reason to downvote.
@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 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.
0

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

Seriously? You suggest using a library for creating arrays?
@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.
-3

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

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 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.
Not bad to use (for .. in .. ) if you understand there to use.
@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 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.
|

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.