陣列Array :concat vs push

Barry YU
2 min readMay 4, 2020

--

Example :

小明接到修改編碼的任務 , 任務是要將某變數的陣列內容, 新增週六及週日 他使用了直覺方法, (當公司看到結果後,也順勢將小明開除)
請問發生什麼事?

使用直覺方法: +=

let day = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
weekend += ['Saturday','Sunday']
console.log(day) // guest what happen ?

正確用法:

Concat

/concatenation /(聯結)

新的變數:newDays

push

/加進去/

變數一樣

concat vs push :

進行陣列操作, 使用者要改變原本的陣列, 要注意是否有重新賦値

concat 是透過新的陣列同時新的物件內容.

push 陣列不改變, 直接修改物件內容

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response