2

Javascript Array Concat Method

 1 year ago
source link: https://hackernoon.com/javascript-array-concat-method
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Javascript Array Concat Method

Notifications
Check what is trending in the tech world today! Tune in to TechBeat 🎵
Yesterday at 4:56 PM
Recently laid off from a tech company? Share your story for free on HackerNoon!
Yesterday at 4:26 PM
Happy Weekend, enjoy these top stories from this week, Cribs, Miriam, and more 💚
Last Saturday at 6:00 PM
New Week, New Chance to Win from $18,000! Enter #EnterTheMetaverse Writing Contest Now!
11/08/2022
Happy Weekend, enjoy these top stories from this week, MARION, BIOLOGY, and more 💚
11/05/2022
Stable Diffusion AI Image Generation is Now Available in the HackerNoon Text Editor!
11/03/2022
Elon Musk now officially owns Twitter! What do you think of this? 👀
10/31/2022
Happy Weekend, enjoy these top stories from this week, Instinct, Comeback, and more 💚
10/29/2022
HN Shareholder Newsletter: Green Clock Strikes Noon :-)
10/26/2022
Start off your week the right way! Here are some must-read top stories from this week, Time Machine, Space, Java Algorithm and more 💚
10/26/2022
Vote now on HackerNoon weekly polls!
10/26/2022
Highlight any text on a story and HackerNoon will generate beautiful quote images for you to share!
10/26/2022
Over 100,000 votes have been casted for this year’s Noonies Nominees. Vote for your favorite now!
10/03/2022
Don't miss out on the daily top trending stories on HackerNoon! Subscribe to TechBeat to see what people are currently interested about!
09/20/2022
The Sandbox is back with #EnterTheMetaverse Writing Contest. Win from $18,000 prize pool!
09/12/2022
HackerNoon now publishes sci-fi! Read some of our science fiction stories today and submit your own!
09/05/2022
$200k+ in Committed Writer Payouts for HackerNoon Writing Contests. Enter to Win Monthly Prizes!
08/29/2022
see 10 more
Javascript Array Concat Method by@smpnjn

Javascript Array Concat Method

November 14th 2022 New Story
3 min
by @smpnjn

Johnny Simpson

@smpnjn

Product, Engineering, Web

Open TLDRtldt arrow
Read on Terminal Reader
Read this story in a terminal

Too Long; Didn't Read

The `concat` method on arrays is used to take two arrays and concatenate them into one. It takes as many arrays as you like - so you can concate many arrays at once. The method creates a shallow copy of the array it's applied to, with the addition of any other arrays added to the end. Nested arrays are concatenated in the same order that the outer array is removed, meaning the outer elements are removed. The basic premise is that the arrays we used in `Concat` maintain the same reference in memory.
featured image - Javascript Array Concat Method
Your browser does not support theaudio element.
Read by Dr. One (en-US)
Audio Presented by

@smpnjn

Johnny Simpson

Product, Engineering, Web

About @smpnjn
LEARN MORE ABOUT @SMPNJN'S EXPERTISE AND PLACE ON THE INTERNET.

The concat method on arrays is used to take two arrays and concatenate them into one. It takes as many arrays as you like - so you can concatenate many arrays at once:

Array.concat(value1, value2, value3, ...valueN)

Each item added to the concat method must be of type array. Let's look at a simple example. Below, we combine two arrays:

let myArray = [ 1, 2, 3, 4, 5 ];
let newArray = [ 'n', 'a', 'e', 'k' ] 

let mergedArray = myArray.concat(newArray);

console.log(mergedArray); // [ 1, 2, 3, 4, 5, 'n', 'a', 'e', 'k' ]

concat does not remove duplicates - but if you are interested in a data structure that does, you might want to read my guide on Javascript sets.

The concat method creates a shallow copy of the array it's applied to, with the addition of any other arrays added to the end.

That means that although it seems like a new array is created, it still has a connection with the original array you modified. In fact, every array you mention in concat is a shallow copy of the original. Consider the following example:

let myArray = [ { name: "John" }, 1, 2, 3, 4, 5 ];
let newArray = [ { name: "Jacob" }, 'a', 'e', 'k' ] 

let mergedArray = myArray.concat(newArray);

myArray[0].name = "Jingleheimer";
newArray[0].name = "Schmidt";
newArray[1] = 5;
console.log(mergedArray, myArray); // [ { name: "Jingleheimer" }, 1, 2, 3, 4, 5, { name: "JSchmidtacob" }, 'a', 'e', 'k' ]

You might expect that changing name on myArray and newArray does not affect mergedArray - but it does. However, newArray[1] = 5 has no effect on mergedArray.

The basic premise is that the arrays we used in concat maintain the same reference in memory as the original arrays. Updating name updates both the mergedArray and the original arrays since they are all stored in the same place. However, typing newArray[1] tells Javascript to put a completely new value in position [1] of `mergedArray.

This is important to remember when you run into weird Javascript array errors.

## Concating multiple arrays. We've looked at how you can concatenate two arrays, but concatenating more works in much the same way. You list all your arrays in the concat function like so:

let myArray = [ 1, 2, 3, 4, 5 ];
let newArray = [ 'n', 'a', 'e', 'k' ] 
let anotherArray = [ 7, 1, 5, 7 ] 

let mergedArray = myArray.concat(newArray, anotherArray);

console.log(mergedArray); // [ 1, 2, 3, 4, 5, 'n', 'a', 'e', 'k', 7, 1, 5, 7 ]

The arrays are merged in the order they are mentioned.

## Concatenating nested arrays. Nested arrays are concatenated in the same way. The outer array is removed, meaning that the nested elements become part of the first array:

let myArray = [ [1, [2, 3], 4], 5 ];
let newArray = [ ['n', 'a'], 'e', 'k' ] 

let mergedArray = myArray.concat(newArray);

console.log(mergedArray); // [ [1, [2, 3], 4], 57, ['n', 'a'], 'e', 'k' ]

## Values in concatenated arrays If you do pass a simple value to concat, it will treat it as an array element. That means if you passed in 'value' rather than ['value'], it would coerce it to ['value'] anyway:

let myArray = [ 1, 2, 3, 4, 5 ];

let mergedArray = myArray.concat(1, 2, 3);

console.log(mergedArray); // [ 1, 2, 3, 4, 5, 1, 2, 3 ]

Also published here.

L O A D I N G
. . . comments & more!

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK