Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

How to Concatenate or Merge Arrays With the Spread Operator in JavaScript

let femaleNames = ["Daniel", "Peter", "Joe"];
let maleNames = ["Sandra", "Lucy", "Jane"];

let allNames = [...femaleNames, ...maleNames];

console.log(allNames); // ["Daniel","Peter","Joe","Sandra","Lucy","Jane"]

// It's also important to know that we can use the same approach for as many arrays
// as we have. We can also add individual elements within the array:

let femaleNames = ["Daniel", "Peter", "Joe"];
let maleNames = ["Sandra", "Lucy", "Jane"];
let otherNames = ["Bill", "Jill"];

let moreNames = [...otherNames, ...femaleNames, ...maleNames];
let names = [...moreNames, "Ben", "Fred"];


// This saves us the stress of using a complicated syntax like the concat() method
Source by www.freecodecamp.org #
 
PREVIOUS NEXT
Tagged: #How #Concatenate #Merge #Arrays #With #Spread #Operator #JavaScript
ADD COMMENT
Topic
Name
6+8 =