开发者

Creating array collection in JavaScript?

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.


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];


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);


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"); 


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.


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];
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜