# Confused. don't understand - [].concat.apply([],arr)

Hi,

I’m on one of the algorithm challenges - “Sorted Union”, where one of the things we have to do is flatten an array of arrays.

E.g.
> var arr = [,,,,,];
> var rslt = Array.concat.apply([],arr);
> console.log(rslt) // [0,1,2,3,4,5]

It seems straightforward but I’m missing something. How does this work?
I looked at the documentation for Array.prototype.concat() on https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat, but still didn’t understand the above code.

I understand a.concat(b) produces [a,b] (e.g. [1,2].concat([3,4]) === [1,2,3,4]),
but how/why does it flatten the array one level?

i.e. Why is Array.concat.apply( [], [1, , [3, 4]] ) === Array.concat(1, , [3,4]) ??? (result is [1,2,3,4]) in both cases

Thanks,

• Jay
2 Likes

there is also `apply` method used there.

`Array.prototype.concat.apply([1,2], [,]) === [1,2,3,4]`
is equal to
`[1,2].concat(,) === [1,2,3,4]`
or in es2015
`[1,2].concat(...[,]) === [1,2,3,4]`

apply

It’s a property from concat() that does the heavy lifting, “apply” just executes the method.

1 Like

So what you’re saying is that the concat function on arrays (i.e. Array.protype.concat ([x]) will always reduce [x] by 1 array level i.e. if arr = [1,2] it will become 1,2 and if arr = [[1,2]] it will become [1,2]).

If I understand correctly, this is a feature of the JavaScript “concat” function - i.e. this happens automatically - but only when using apply to call the function.
So, Array.prototype.concat.apply(this, [x]) === this.concat([x]), where “this” is the first argument to the concat function.

Is my understanding correct?

Interesting, I have not seen this feature in other languages.

Thank you @marzelin and @Selhar1

Ahh, thanks @MarkoN95, this makes more sense now.

Ok, I think I got it.

Array.prototype.concat.call (this, [x]) === this.concat([x]),

and

Array.prototype.concat.apply (this, [x]) === this.concat(x).

And this is so because “apply” takes an array of arguments and treats each element of that array as a single argument.

So, an unintended consequence of “apply” is to unravel one level of the array - and this has absolutely nothing to do with the concat function which simply concatenates 2 values.

Sorry if I’m beinig overly redundant and simplistic in my understanding (still trying to learn JavaScript) A new use for Apply
{"ft_lang_label":"__label__en","ft_lang_prob":0.7109598,"math_prob":0.9060402,"size":796,"snap":"2020-34-2020-40","text_gpt3_token_len":245,"char_repetition_ratio":0.116161615,"word_repetition_ratio":0.0,"special_character_ratio":0.36809045,"punctuation_ratio":0.28773585,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.98746467,"pos_list":[0,1,2,3,4,5,6],"im_url_duplicate_count":[null,null,null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-09-18T18:45:19Z\",\"WARC-Record-ID\":\"<urn:uuid:70d2b08b-8077-4e4d-af33-f60c3b1b8077>\",\"Content-Length\":\"36780\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:e43a5cfd-7e82-4c4e-9b1b-6ce8538a9d15>\",\"WARC-Concurrent-To\":\"<urn:uuid:556659c5-f103-4060-b031-f1175dcc390d>\",\"WARC-IP-Address\":\"64.71.168.201\",\"WARC-Target-URI\":\"https://forum.freecodecamp.org/t/confused-dont-understand-concat-apply-arr/15180\",\"WARC-Payload-Digest\":\"sha1:UUKS7J3NTN6MLV5EPLURG5LAQ45OXEJF\",\"WARC-Block-Digest\":\"sha1:HPGVVZSYK4DVOC36VZK4FJECWFE5G7IY\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-40/CC-MAIN-2020-40_segments_1600400188049.8_warc_CC-MAIN-20200918155203-20200918185203-00609.warc.gz\"}"}