Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaScript:如何在数组中存储对象?_Javascript_Arrays_Object - Fatal编程技术网

JavaScript:如何在数组中存储对象?

JavaScript:如何在数组中存储对象?,javascript,arrays,object,Javascript,Arrays,Object,如何将这些对象放入甲板阵列中?抱歉,如果这是一个愚蠢的问题,我很难找到答案。如评论中所述,您有一些选择 1) 使用以下对象实例化数组: function CreateSuit(suit){ this.suit = suit; this.vaule = i; this.name = name; } var twoClubs = new Card ('clubs', 2, 'two of clubs'); var threeClubs = new Card ('clubs', 3, '

如何将这些对象放入甲板阵列中?抱歉,如果这是一个愚蠢的问题,我很难找到答案。

如评论中所述,您有一些选择

1) 使用以下对象实例化数组:

function CreateSuit(suit){
  this.suit = suit;
  this.vaule = i;
  this.name = name;
}

var twoClubs = new Card ('clubs', 2, 'two of clubs');
var threeClubs = new Card ('clubs', 3, 'three of clubs');
var fourClubs = new Card ('clubs', 4, 'four of clubs');

var deck = [];
2) 将对象添加到阵列中:

var deck = [twoClubs, threeClubs, fourClubs];
3) 您甚至可以实例化数组并同时声明对象:

var deck = [];
deck.push(twoClubs);
deck.push(threeClubs);
deck.push(fourClubs);

从技术上讲,这是最有效的方法(注意:这取决于浏览器/实现)。

如评论中所述,您有几个选项

1) 使用以下对象实例化数组:

function CreateSuit(suit){
  this.suit = suit;
  this.vaule = i;
  this.name = name;
}

var twoClubs = new Card ('clubs', 2, 'two of clubs');
var threeClubs = new Card ('clubs', 3, 'three of clubs');
var fourClubs = new Card ('clubs', 4, 'four of clubs');

var deck = [];
2) 将对象添加到阵列中:

var deck = [twoClubs, threeClubs, fourClubs];
3) 您甚至可以实例化数组并同时声明对象:

var deck = [];
deck.push(twoClubs);
deck.push(threeClubs);
deck.push(fourClubs);

从技术上讲,这是最有效的方法(注意:这取决于浏览器/实现)。

有几种方法可以做到这一点。可以使用存在的值初始化数组:

var deck = [new Card ('clubs', 2, 'two of clubs'), new Card ('clubs', 3, 'three of clubs'), new Card ('clubs', 4, 'four of clubs')];
或者您可以动态地将它们添加到阵列中:

var deck = [twoClubs, threeClubs, fourClubs]
或者,您甚至可以指定要在数组中放置它们的位置:

var deck = [];
deck.push(twoClubs);
deck.push(threeClubs);
deck.push(fourClubs);
或者您可以混合搭配以下任何一种:

var deck = [];
deck[2] = threeClubs;
deck[0] = fourClubs;
deck[1] = twoClubs

有几种方法可以做到这一点。可以使用存在的值初始化数组:

var deck = [new Card ('clubs', 2, 'two of clubs'), new Card ('clubs', 3, 'three of clubs'), new Card ('clubs', 4, 'four of clubs')];
或者您可以动态地将它们添加到阵列中:

var deck = [twoClubs, threeClubs, fourClubs]
或者,您甚至可以指定要在数组中放置它们的位置:

var deck = [];
deck.push(twoClubs);
deck.push(threeClubs);
deck.push(fourClubs);
或者您可以混合搭配以下任何一种:

var deck = [];
deck[2] = threeClubs;
deck[0] = fourClubs;
deck[1] = twoClubs

现在,使用其他答案中提到的任何一种方法在数组中添加对象

var deck = [threeClubs];
deck[1] = twoClubs;
deck.push(fourClubs);

您可以使用从数组中检索并删除最后一个对象

var deck = [];
deck.push(twoClubs);
deck.push(threeClubs);
deck.push(fourClubs);
或 您可以使用索引从特定位置检索对象

deck.pop(); // remove and return last object

现在,使用其他答案中提到的任何一种方法在数组中添加对象

var deck = [threeClubs];
deck[1] = twoClubs;
deck.push(fourClubs);

您可以使用从数组中检索并删除最后一个对象

var deck = [];
deck.push(twoClubs);
deck.push(threeClubs);
deck.push(fourClubs);
或 您可以使用索引从特定位置检索对象

deck.pop(); // remove and return last object

deck.push(两个球杆)
var deck=[TwoClub,ThreeClub,FourClub]
deck.push(两个球杆)
var deck=[TwoClub,ThreeClub,FourClub]太棒了谢谢大家的帮助!这给了我一些不错的选择!太棒了,谢谢大家的帮助!这给了我一些不错的选择!“从技术上讲,这是最有效的方法。”根据什么标准?不需要引用。通过不在数组外声明变量,OP将执行更少的操作并使用更少的内存。或者,运行它们的引擎可以将它们重写为完全相同的内容。(我认为这似乎是有可能的。)我会主张在网络请求中发送更少的字节:P“从技术上讲,这是最有效的方式。”根据什么标准?不需要引用。通过不在数组外声明变量,OP将执行更少的操作并使用更少的内存。或者,运行它们的引擎可以将它们重写为完全相同的内容。(我认为这似乎是有可能的。)我会主张在网络请求中发送更少的字节:P