将两个数组合并到一个对象中[重复]

时间:2021-06-26 12:20:40

This question already has an answer here:

这个问题在这里已有答案:

So I am trying to merge two arrays into one object. More specifically I am creating a deck of cards using JavaScript. I have created the two arrays (shown below) and am looking for some help on how to merge them so that the new object will be formatted like this {suit: 'hearts', value: 'A'}. I believe i need to do a for loop but haven't been able to make it work. Anyone have any suggestions?

所以我试图将两个数组合并为一个对象。更具体地说,我正在使用JavaScript创建一副牌。我创建了两个数组(如下所示),我正在寻找一些关于如何合并它们的帮助,以便新对象的格式如下{suit:'hearts',value:'A'}。我相信我需要做一个for循环但是无法使它工作。有人有什么建议吗?

// ... trying to merge the two arrays here ...

// ...试图在这里合并两个数组......

  function deck_o_cards() {
  var values = [2, 3, 4, 5, 6, 7, 8, 9, 10, 'J', 'Q', 'K', 'A'];
  var suits = ['hearts', 'diamonds', 'clubs', 'spades'];

  var cards = [ ];
    for(i=0, i< suits.length, i++){
        var newSuit = suits[i];
        for(a=0; a< values.length, a++) {
            var newValue= values[a];
            newArray=[newSuit, newValue];
            cards.push(newArray);
        }
    }
}

2 个解决方案

#1


You will need to iterate over the values array using a loop and then inside each iteration also iterate over the suits array in a loop. You will need two different indexes to make the nested loops work.

您需要使用循环遍历values数组,然后在每次迭代内部也遍历一个循环中的suit数组。您将需要两个不同的索引来使嵌套循环工作。

The arrays are zero indexed so the index will begin at zero and stop when it index reaches the length of the array.

数组为零索引,因此索引将从零开始,并在索引达到数组长度时停止。

Here is an example of a nested loop but you should use the array length property rather than 13 and 4.

下面是嵌套循环的示例,但您应该使用数组长度属性而不是13和4。

for(var i = 0; i < 13; i++) {
  for(var j = 0; i < 4; i++) {
    ...
  }
}

Inside the inner loop you can create your card object with the value from each array using the separate indexes and add it to your card array by assigning it to cards[cards.length] = { } which adds a new item to the end of the array.

在内部循环内部,您可以使用单独的索引使用每个数组中的值创建卡对象,并将其添加到卡阵列中,方法是将其分配给卡片[cards.length] = {},这会将新项添加到卡的末尾。阵列。

You can also accomplish the same thing using the Array.map() and Array.push() functions to replace the loop and assignment operations.

您还可以使用Array.map()和Array.push()函数来完成相同的操作,以替换循环和赋值操作。

#2


You are almost there. The problem lies with the code inside your loop. You are creating an array when you actually desire an object. Try this inside your loop:

你快到了。问题在于循环中的代码。当你真正想要一个对象时,你正在创建一个数组。在循环中尝试这个:

var newValue = values[a];
var newCard = {suit: newSuit, value: newValue};
cards.push(newCard);

Also note the use of var newCard instead of newArray. var avoids adding an object to the global scope.

另请注意使用var newCard而不是newArray。 var避免将对象添加到全局范围。

#1


You will need to iterate over the values array using a loop and then inside each iteration also iterate over the suits array in a loop. You will need two different indexes to make the nested loops work.

您需要使用循环遍历values数组,然后在每次迭代内部也遍历一个循环中的suit数组。您将需要两个不同的索引来使嵌套循环工作。

The arrays are zero indexed so the index will begin at zero and stop when it index reaches the length of the array.

数组为零索引,因此索引将从零开始,并在索引达到数组长度时停止。

Here is an example of a nested loop but you should use the array length property rather than 13 and 4.

下面是嵌套循环的示例,但您应该使用数组长度属性而不是13和4。

for(var i = 0; i < 13; i++) {
  for(var j = 0; i < 4; i++) {
    ...
  }
}

Inside the inner loop you can create your card object with the value from each array using the separate indexes and add it to your card array by assigning it to cards[cards.length] = { } which adds a new item to the end of the array.

在内部循环内部,您可以使用单独的索引使用每个数组中的值创建卡对象,并将其添加到卡阵列中,方法是将其分配给卡片[cards.length] = {},这会将新项添加到卡的末尾。阵列。

You can also accomplish the same thing using the Array.map() and Array.push() functions to replace the loop and assignment operations.

您还可以使用Array.map()和Array.push()函数来完成相同的操作,以替换循环和赋值操作。

#2


You are almost there. The problem lies with the code inside your loop. You are creating an array when you actually desire an object. Try this inside your loop:

你快到了。问题在于循环中的代码。当你真正想要一个对象时,你正在创建一个数组。在循环中尝试这个:

var newValue = values[a];
var newCard = {suit: newSuit, value: newValue};
cards.push(newCard);

Also note the use of var newCard instead of newArray. var avoids adding an object to the global scope.

另请注意使用var newCard而不是newArray。 var避免将对象添加到全局范围。