在 JavaScript 中创建数组集合?

发布于 2024-10-26 14:58:30 字数 59 浏览 1 评论 0原文

是否可以在 JavaScript 中创建数组集合?如果是这样,我该怎么做?如果我有一些数据:例如1-5。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

若有似无的小暗淡 2024-11-02 14:58:43

您可以创建对象:

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

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

访问您可以访问的所有数据

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

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

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];
}
樱桃奶球 2024-11-02 14:58:42

这是创建数组集合的简单方法

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

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);
芯好空 2024-11-02 14:58:42

javascript 数组的 AZ

检查此链接

如果你想填充一个数组

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

或者

var tmpArray= new Array ("a", "b", "c", "d"); 

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"); 
初见终念 2024-11-02 14:58:42

如果您想要更复杂的数据集合,可以使用 data.js 抽象库。

如果您更具体地想要什么,我可以给您举一个例子。

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.

黑色毁心梦 2024-11-02 14:58:41

我实际上不太明白你想做什么,但你可以创建一个包含数字 1 到 5 的 Javascript 数组,如下所示:

var myArray = [1, 2, 3, 4, 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];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文