It may sounds like a silly question but
var arr1 = ['1', '2'];
var arr2 = ['3', '4'];
console.log(arr1.concat(arr2));
will output ["1", "2", "3", "4"]
But
var arr1 = ['1', '2'];
console.log(
arr1.concat(document.getElementsByTagName('h1'))
);
will output ["1", "2", HTMLCollection[2]]
How do I concatenate the html collection with an array ?
One way to convert an HTMLCollection to a JavaScript array is to use the slice method. We get divs by call querySelector to select all the divs in the HTML. We just call slice with divs and it'll return the div element objects in an array. We call slice on an empty array and pass in the divs HTML.
Array. from() method will create a new Array instance from an array-like or iterable object. So it can be used both on NodeList and HTMLCollection . This is a clear and straightforward method to create an Array , and not only from NodeList or HTMLCollection .
An HTMLCollection is not an Array! An HTMLCollection may look like an array, but it is not. You can loop through an HTMLCollection and refer to its elements with an index. But you cannot use Array methods like push(), pop(), or join() on an HTMLCollection.
You need to convert the HTMLCollection
into an array. The best way of doing this in modern Javascript is Array.from
. This converts any array-like object (or other iterable value) into a real Javascript array.
arr1.concat(Array.from(document.getElementsByTagName('h1')))
You can use push.apply
, like so:
var arr1 = ['1', '2'];
arr1.push.apply(arr1, document.getElementsByTagName('h1'));
console.log(arr1);
<h1>x</h1>
<h1>y</h1>
And in ES2015+ (aka ES6+), you can use push
with spread notation:
var arr1 = ['1', '2'];
arr1.push(...document.getElementsByTagName('h1'));
console.log(arr1);
<h1>x</h1>
<h1>y</h1>
Note that in both cases, this is different from concat
in that it avoids creating a new array (it just modifies the original one referenced by arr1
). Whether that difference is a good thing (not creating unnecessary objects) or a bad thing (I wanted a new array!) depends on your use case. :-)
You can use Array.from, however it's fairly new so may not be supported by all the browsers you need. Some alternatives, the last of which will be supported by all browsers in use:
Runnable snippet below.
var arr = [0,1];
var divs = document.querySelectorAll('div');
// Use spread syntax, pretty new though
var spread = arr.concat(...divs);
console.log('Spread\n' + spread.join('\n'));
// Convert to array using slice, then concat
var sliced = Array.prototype.slice.call(divs).concat(arr);
console.log('Sliced\n' + sliced.join('\n'));
// Convert to array using map, then concat
var mapped = Array.prototype.map.call(divs, function(div){return div}).concat(arr);
console.log('Mapped\n' + mapped.join('\n'));
// Call concat with apply directly using concat
var conned = arr.concat.apply(arr, divs);
console.log('Conned\n' + conned.join('\n'));
<div>div1</div>
<div>div2</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With