JavaScript Array Concat Not Working. Why

Javascript Array Concat not working. Why?

The concat method doesn't change the original array, you need to reassign it.

if ( ref instanceof Array )
this.refs = this.refs.concat( ref );
else
this.refs.push( ref );

concat does not join JavaScript arrays together?

.concat creates a new Array. You need to overwrite the old one.

scriptUrls = scriptUrls.concat(urls);

Or if you want to keep the original scriptUrls Array, you can .push() the values in.

scriptUrls.push.apply(scriptUrls, urls);

This uses .apply() to convert urls into individual arguments passed to .push(). This way the content of urls is added to scriptUrls as individual items.


Also, note that .concat() flattens the Array. If you wanted an Array of Arrays, then you'd use scriptUrls.push(urls).

Why can't I concatenate these two arrays?

From documentation - The concat() method is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array.

One way to use concat would be to do the following

state.building.panoramas = state.building.panoramas.concat(panoramas)

Or you can simply

[].push.apply(state.building.panoramas, panoramas);

Javascript concat not working as expected - not merging

Declare array before for loop, set fixed.innerHtml after and read about javascript scope.

var fixed = document.getElementById('fixed');
var sections = document.getElementsByClassName('section');
var arr = [];
for (i = 0; i < sections.length; i++) { arr = arr.concat(sections[i]);}
fixed.innerHTML = arr;
<div class="fixed" id="fixed"></div><div class="section"></div><div class="section"></div><div class="section"></div><div class="section"></div><div class="section"></div>

JavaScript concat not working as expected, care to elaborate?

As per documentation of Array.prototype.concat:

Returns a new array comprised of this array joined with other array(s)
and/or value(s).

Which means it does not modify the object it is applied to.

Change to:

allCities = allCities.concat(cities[c]);

javascript recursive function concat not working

Array.prototype.concat()

The concat() method is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array.

So those lines do nothing:

if("sp" in val) values.concat(getValues(val.sp))
else if("it" in val) values.concat(getValues(val.it))

You need to write:

if("sp" in val) values = values.concat(getValues(val.sp))
else if("it" in val) values = values.concat(getValues(val.it))

And you should not use map if you don't use it's result. Use forEach instead.

Why is Array.push() throwing an error but Array.concat() does not?

.concat returns a new array with both the current items and the new ones where .push modifies the current array instead of returning a new one.

If that array was initially loaded from vuex it results in an unwanted mutation of the store.

Edit:

In the store

mutations: {
addFile(state, file) {
state.ItemData[0].Files.push(file);
},
}

In the component

NewFiles.forEach(file => store.commit("addFile", file));


Related Topics



Leave a reply



Submit