lundi 1 juillet 2019

JavaScript: Test if input args are arrays; If yes, then merge arrays into one big array [duplicate]

This question already has an answer here:

I have the following variables which are arrays:

const gumBrands = ['orbit', 'trident', 'chiclet', 'strident'];

const mintBrands = ['altoids', 'certs', 'breath savers', 'tic tac'];

Below I have the following function that uses the variables as input arguments:

function shallowCopy (arrOne, arrTwo) {

    if (arrOne.constructor === 'Array'){

        return [...arrOne, ...arrTwo]; 
    }

    else {
        console.log('test this'); 
    }
}


shallowCopy(gumBrands, mintBrands)

I am expecting my code to return:

[ 'orbit',
  'trident',
  'chiclet',
  'strident',
  'altoids',
  'certs',
  'breath savers',
  'tic tac' ]

Instead the code runs my else statement and returns: test this

What am I doing wrong?

Aucun commentaire:

Enregistrer un commentaire