I am facing a challenge with combining two arrays into one new array that merges the objects of each array together. While I know how to merge two arrays into a single array, I am struggling to combine the actual objects into a brand new object within that array. Below, you can find an example of the input and desired output that I am aiming for:
Given arrays:
array1 = ['1', '2', '3'];
array2 = ['a', 'b', 'c'];
The desired output array should be:
array3 = ['1a', '2b', '3c'];
My current attempt involves creating a deck of cards for a game of euchre, which requires cards with values like 9, 10, Jack, Queen, and Ace, and suits like Clubs, Diamonds, Hearts, and Spades, resulting in a total of 24 card combinations. I have set up two arrays - one for the card 'values' and one for the card 'suits.' Through my function, I intend to pass both of these arrays into a for loop that will add the object of the 'suits' array to the end of the 'values' array. The new list will then be stored in the empty 'deck' array to generate the final combined array. However, I am encountering an error message stating "Uncaught TypeError: Cannot read properties of undefined (reading 'length')" at the beginning of my 'createDeck' function and when I attempt to call the function using a console log statement at the end. I would greatly appreciate any guidance on the logic required to successfully merge these arrays, as I am relatively new to working with functions and arrays.
const values = ['9', '10', 'J', 'Q', 'K','A'];
const suits = ['C', 'D', 'H', 'S'];
function createDeck(values, suits) {
let deck = [];
for (let i = 0; i < suits.length; i++) {
for (let x = 0; x < values.length; x++) {
let card = {Value: values[x], Suit: suits[i]};
deck.push(card);
}
}
return deck;
}
console.log(createDeck());