Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

220
Views
I wrote a function to merge two arrays into a single array but I'm getting "Cannot read properties of undefined"

I am trying to take 2 arrays and merge them into one new array that will combine the objects of each array. I understand how to combine 2 arrays into 1 large one, but I'm having trouble combining the actual objects into a new object within that array. An example of the input and intended output I am trying to get is listed below:

Given arrays:

array1 = ['1','2','3'];

array2 = ['a','b','c'];

New outputted array should be:

array3 = ['1a','2b','3c'];

Here is what i have attempted to do so far. In this example, I am trying to create the deck of cards needed to play a game of euchre (should have the cards 9, 10, Jack, Queen, and Ace and the suits of Clubs, Diamonds, Hearts, and Spades for each card for a total of 24 card combinations). I created 2 arrays (one for the card 'value' and one for the card 'suits.' For my function, I am passing both of these decks into a for loop that will ideally append the object of the 'suits' array to the end of the 'values' array. This new list is then passed into the empty 'deck' array to return the new array. However, I am getting and "Uncaught TypeError: Cannot read properties of undefined (reading 'length')." This is occuring at the beginning of my function 'createDeck' and when I try to call the function in the console log statement at the bottom. Any assistance in understanding the logic I would need to use to combine these arrays would be greatly appreciated as I am just beginning to work 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());
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You can create a new array with the map method.

const values = ['1','2', '3'];
const suits =  ['a', 'b', 'c'];

function createDeck() {
  let customArray = [];
  
  // check for equality of array lengths
  if (values.length === suits.length) {
    // create a new array with the map method
    customArray = values.map((value, index) => {
      return value + suits[index]
    })
  }

  return customArray;
}

console.log(createDeck())
about 4 years ago · Juan Pablo Isaza Report

0

Basically you can use Array.prototype.map() method to achieve your expected results. Length of arrays is assumed equal to each other. A sample code is given as below:

array1 = ['1','2','3'];
array2 = ['a','b','c'];
combineArrays = (arr1,arr2) => {
    const arr = arr1.map((elem,index)=> {
        elem += arr2[index];
        return elem
    })
    return arr
}
console.log(combineArrays(array1, array2))  // ['1a','2b','3c']

For loop version is:

array1 = ['1','2', '3']
array2 = ['a', 'b', 'c']
combineArrays = (arr1,arr2) => {
    const deck = []
    for(i=0; i<arr1.length; i++) {
        deck.push(arr1[i] + arr2[i])
    }
    return deck
}
console.log(combineArrays(array1, array2))  // ['1a','2b','3c']

about 4 years ago · Juan Pablo Isaza Report

0

try to declare a new array and in the for loop do

array[i] = values[i] + suits[i]

maybe you need to add "" in the array with strings

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!